From e77f6a99a2e3e8a63959d4d2f0fa7b122d5857b5 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Thu, 24 Feb 2011 17:45:29 -0500 Subject: [PATCH] Kostache not extending Mustache, aka Kostache v2.0beta1 --- classes/kohana/kostache.php | 223 ++++++++++++++++++----------- classes/kohana/kostache/layout.php | 31 ++++ classes/kostache.php | 5 +- classes/kostache/layout.php | 3 + classes/view/kohana/layout.php | 40 ------ classes/view/layout.php | 6 - 6 files changed, 175 insertions(+), 133 deletions(-) create mode 100644 classes/kohana/kostache/layout.php create mode 100644 classes/kostache/layout.php delete mode 100644 classes/view/kohana/layout.php delete mode 100644 classes/view/layout.php diff --git a/classes/kohana/kostache.php b/classes/kohana/kostache.php index 9d4be4c..7aac072 100644 --- a/classes/kohana/kostache.php +++ b/classes/kohana/kostache.php @@ -1,51 +1,121 @@ - $class)); + { + throw new Kohana_Exception('View class does not exist: :class', array( + ':class' => $class, + )); + } - return new $class($template, $view, $partials); + return new $class($template, $partials); } /** - * Kostache class constructor. - * - * This method accepts a $template string and a $view object. Optionally, pass an associative - * array of partials as well. - * - * @access public - * @param string $template (default: null) - * @param mixed $view (default: null) - * @param array $partials (default: null) - * @return void + * @var string Mustache template */ - public function __construct($template = null, $view = null, $partials = null) - { - parent::__construct($template, $view, $partials); + protected $_template; - $this->_charset = Kohana::$charset; + /** + * @var array Mustache partials + */ + protected $_partials = array(); + + /** + * Loads the template and partial paths. + * + * @param string template path + * @param array partial paths + * @return void + * @uses Kostache::template + * @uses Kostache::partial + */ + public function __construct($template, array $partials = NULL) + { + // Load the template + $this->template($template); + + if ($partials) + { + foreach ($partials as $name => $path) + { + // Load the partial + $this->partial($name, $path); + } + } + } + + /** + * Magic method, returns the output of [Kostache::render]. + * + * @return string + * @uses Kostache::render + */ + public function __toString() + { + try + { + $this->render(); + } + catch (Exception $e) + { + ob_start(); + + // Render the exception + Kohana::exception_text($e); + + return (string) ob_get_clean(); + } + } + + /** + * Loads a new template from a path. + * + * @return Kostache + */ + public function template($path) + { + $this->_template = $this->_load($path); + + return $this; + } + + /** + * Loads a new partial from a path. If the path is empty, the partial will + * be removed. + * + * @param string partial name + * @param mixed partial path, FALSE to remove the partial + * @return Kostache + */ + public function partial($name, $path) + { + if ( ! $path) + { + unset($this->_partials[$name]); + } + else + { + $this->_partials[$name] = $this->_load($path); + } + + return $this; } /** @@ -101,64 +171,47 @@ class Kohana_Kostache extends Mustache } /** - * Magic method, returns the output of [View::render]. + * Renders the template using the current view. * * @return string - * @uses View::render */ - public function __toString() + public function render() { - try - { - return $this->render(); - } - catch (Exception $e) - { - // Display the exception message - Kohana::exception_handler($e); - - return ''; - } + return $this->_stash($this->_template, $this, $this->_partials)->render(); } - public function render($template = null, $view = null, $partials = null) + /** + * Return a new Mustache for the given template, view, and partials. + * + * @param string template + * @param Kostache view object + * @param array partial templates + * @return Mustache + */ + protected function _stash($template, Kostache $view, array $partials) { - if (NULL === $template) - { - // Override the template location to match kohana's conventions - if ( ! $this->_template) - { - $foo = explode('_', get_class($this)); - array_shift($foo); - $view_location = strtolower(implode('/', $foo)); - } - else - { - $view_location = $this->_template; - } - - $this->_template = Kohana::find_file('templates', $view_location, 'mustache'); - - if ( ! $this->_template AND ! $template) - throw new Kohana_Exception('Template file not found: templates/'.$view_location); - - $this->_template = file_get_contents($this->_template); - } - - // Convert partials to expanded template strings - if ( ! $this->_partials_processed) - { - foreach ($this->_partials as $key => $partial_template) - { - if ($location = Kohana::find_file('templates', $partial_template, 'mustache')) - { - $this->_partials[$key] = file_get_contents($location); - } - } - - $this->_partials_processed = TRUE; - } - - return parent::render($template, $view, $partials); + return new Mustache($template, $view, $partials); } + + /** + * Load a template and return it. + * + * @param string template path + * @return string + * @throws Kohana_Exception if the template does not exist + */ + protected function _load($path) + { + $file = Kohana::find_file('templates', $path, 'mustache'); + + if ( ! $file) + { + throw new Kohana_Exception('Template file does not exist: :path', array( + ':path' => 'templates/'.$path, + )); + } + + return file_get_contents($file); + } + } diff --git a/classes/kohana/kostache/layout.php b/classes/kohana/kostache/layout.php new file mode 100644 index 0000000..295aab7 --- /dev/null +++ b/classes/kohana/kostache/layout.php @@ -0,0 +1,31 @@ +_layout) + { + return parent::render(); + } + + $partials = $this->_partials; + + $partials[Kostache_Layout::CONTENT_PARTIAL] = $this->_template; + + $template = $this->_load($this->_layout); + + return $this->_stash($template, $this, $partials)->render(); + } + +} diff --git a/classes/kostache.php b/classes/kostache.php index 5303c22..2595419 100644 --- a/classes/kostache.php +++ b/classes/kostache.php @@ -1,2 +1,3 @@ -_template) - { - $foo = explode('_', get_class($this)); - array_shift($foo); - $this->_template = strtolower(implode(DIRECTORY_SEPARATOR, $foo)); - } - - if ($this->render_layout) - { - $this->_partials+=array( - 'body' => $this->_template - ); - - // Make the layout view the child class's template - $this->_template = $this->_layout; - } - - return parent::render($template, $view, $partials); - } - -} // End View_Layout diff --git a/classes/view/layout.php b/classes/view/layout.php deleted file mode 100644 index e36f909..0000000 --- a/classes/view/layout.php +++ /dev/null @@ -1,6 +0,0 @@ -