diff --git a/README.md b/README.md index 531623f..9e66b86 100644 --- a/README.md +++ b/README.md @@ -91,13 +91,14 @@ Parameter managers are used to consistently store variables for use in the theme Returns an array of parameters. -* __$pm->getParam($key)__ +* __$pm->getParam($key, $fallback = undefined)__ * string _$key_ - The key for the value + * mixed _$fallback_ - Returns the value stored with $key. + Returns the value stored with $key. The fallback is activated through func_num_args() so it works even when using `null`. - __Throws:__ _\OutOfBoundsException_ - If the value was not set + __Throws:__ _\OutOfBoundsException_ - If the value was not set and a fallback is not available * __$pm->setParam($key, $value)__ @@ -116,6 +117,8 @@ Parameter managers are used to consistently store variables for use in the theme The View returned by the Builder is usually a custom object extending \Foolz\Theme\View. +There's also a bunch of shortcuts to go to the above levels available (getBuilder, getBuilderParamManager, getTheme, getAssetManager) + __It's compulsory to override the `toString()` method (not the `__toString()` magic method!) in order to output the HTML. This function should output HTML or return a string.__ * __$view->toString()__ diff --git a/classes/Foolz/Theme/Builder.php b/classes/Foolz/Theme/Builder.php index 7ef3b4c..5e8f853 100644 --- a/classes/Foolz/Theme/Builder.php +++ b/classes/Foolz/Theme/Builder.php @@ -132,7 +132,7 @@ public function getLayout() */ public function getPartial($name) { - if (isset($this->partials[$name])) + if (array_key_exists($name, $this->partials)) { return $this->partials[$name]; } @@ -140,6 +140,18 @@ public function getPartial($name) throw new \OutOfBoundsException('No such partial exists.'); } + /** + * Tells if a partial has already been created + * + * @param string $name The name of the partial + * + * @return bool True if the partial has been set, false otherwise + */ + public function isPartial($name) + { + return array_key_exists($name, $this->partials); + } + /** * Shorthand for building the layout * diff --git a/classes/Foolz/Theme/ParamManager.php b/classes/Foolz/Theme/ParamManager.php index b7bdb0f..d45a1c9 100644 --- a/classes/Foolz/Theme/ParamManager.php +++ b/classes/Foolz/Theme/ParamManager.php @@ -33,7 +33,8 @@ public function getParams() } /** - * Returns the parameter with the key + * Returns the parameter with the key. Supports fallback. + * It uses func_num_args() so if a second parameter is passed, it will use it as fallback. * * @param string $key * @return mixed The value of the parameter @@ -42,12 +43,18 @@ public function getParams() */ public function getParam($key) { - if ( ! array_key_exists($key, $this->params)) + if (array_key_exists($key, $this->params)) { - throw new \OutOfBoundsException('Undefined parameter.'); + return $this->params[$key]; } - return $this->params[$key]; + // return a second parameter + if (func_num_args() === 2) + { + return func_get_arg(1); + } + + throw new \OutOfBoundsException('Undefined parameter.'); } /** diff --git a/classes/Foolz/Theme/View.php b/classes/Foolz/Theme/View.php index a2d27cb..78eb96b 100644 --- a/classes/Foolz/Theme/View.php +++ b/classes/Foolz/Theme/View.php @@ -45,6 +45,8 @@ class View * @param \Foolz\Theme\Builder $builder The Builder object creating this view * @param string $type The type of view, it can be partial or layout * @param string $view The name of the view + * + * @return \Foolz\Theme\View The new view */ public static function forge(\Foolz\Theme\Builder $builder, $type, $view) { @@ -89,6 +91,36 @@ public function getBuilder() return $this->builder; } + /** + * Returns the global parameter manager located in the builder + * + * @return \Foolz\Theme\ParamManager The ParamManager that belongs to the Builder + */ + public function getBuilderParamManager() + { + return $this->getBuilder()->getParamManager(); + } + + /** + * Returns the theme that belongs to this view + * + * @return \Foolz\Theme\Theme The Theme that created this View + */ + public function getTheme() + { + return $this->getBuilder()->getTheme(); + } + + /** + * Returns the asset manager that belongs to the theme that created this view + * + * @return \Foolz\Theme\AssetManager The Theme's AssetManager + */ + public function getAssetManager() + { + return $this->getTheme()->getAssetManager(); + } + /** * Set the type of view * @@ -189,36 +221,12 @@ public function doBuild() } /** - * If not extended, it will check if there's a classic view file and return the result + * Method to override to echo the content of the theme * - * @return string The compiled string - * @throws \OutOfBoundsException If the view is not found also in the extended themes + * @throws \BadMethodCallException If not overridden */ public function toString() { - $theme = $this->getBuilder()->getTheme(); - - while (true) - { - $file = $theme->getDir().$this->type.DIRECTORY_SEPARATOR.$this->view.'.php'; - if (file_exists($file)) - { - // isolation function - $function = function() - { - ob_start(); - include $this->getTheme()->getDir().$this->type.DIRECTORY_SEPARATOR.$this->view.'.php'; - return ob_get_clean(); - }; - - $function = $function->bindTo($this); - return $function(); - } - - $theme = $theme->getExtended(); - } - - // this should be thrown by $theme->getExtended, but we want to be explicit - throw new \OutOfBoundsException; + throw new \BadMethodCallException('The toString() method must be overridden to output the theme content'); } } \ No newline at end of file