From e77f6a99a2e3e8a63959d4d2f0fa7b122d5857b5 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Thu, 24 Feb 2011 17:45:29 -0500 Subject: [PATCH 1/6] 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 @@ - Date: Thu, 24 Feb 2011 18:00:13 -0500 Subject: [PATCH 2/6] Forgot to actually make Kostache not extend Mustache, DOH! --- classes/kohana/kostache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/kohana/kostache.php b/classes/kohana/kostache.php index 7aac072..1205d19 100644 --- a/classes/kohana/kostache.php +++ b/classes/kohana/kostache.php @@ -1,6 +1,6 @@ Date: Thu, 24 Feb 2011 18:20:05 -0500 Subject: [PATCH 3/6] Load partials defined by view in __construct --- classes/kohana/kostache.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/classes/kohana/kostache.php b/classes/kohana/kostache.php index 1205d19..c5054c2 100644 --- a/classes/kohana/kostache.php +++ b/classes/kohana/kostache.php @@ -51,6 +51,15 @@ class Kohana_Kostache { // Load the template $this->template($template); + if ($this->_partials) + { + foreach ($this->_partials as $name => $path) + { + // Load the partials defined in the view + $this->partial($name, $path); + } + } + if ($partials) { foreach ($partials as $name => $path) From 3919aab0969929b6abdb95d57ddf06544f407258 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Thu, 24 Feb 2011 18:34:41 -0500 Subject: [PATCH 4/6] Make all Kostache classes abstract --- classes/kohana/kostache.php | 2 +- classes/kohana/kostache/layout.php | 2 +- classes/kostache.php | 2 +- classes/kostache/layout.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/classes/kohana/kostache.php b/classes/kohana/kostache.php index c5054c2..8fa6bf3 100644 --- a/classes/kohana/kostache.php +++ b/classes/kohana/kostache.php @@ -1,6 +1,6 @@ Date: Fri, 25 Feb 2011 10:59:43 -0500 Subject: [PATCH 5/6] Added $render_layout flag to Kostache_Layout --- classes/kohana/kostache/layout.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/classes/kohana/kostache/layout.php b/classes/kohana/kostache/layout.php index e3b31d6..ac2dc1c 100644 --- a/classes/kohana/kostache/layout.php +++ b/classes/kohana/kostache/layout.php @@ -7,6 +7,11 @@ abstract class Kohana_Kostache_Layout extends Kostache { */ const CONTENT_PARTIAL = 'content'; + /** + * @var boolean render template in layout? + */ + public $render_layout = TRUE; + /** * @var string layout path */ @@ -14,7 +19,7 @@ abstract class Kohana_Kostache_Layout extends Kostache { public function render() { - if ( ! $this->_layout) + if ( ! $this->render_layout) { return parent::render(); } From 6cc9fa9dcd79290b4fe45560bddeaa08ce004858 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Fri, 25 Feb 2011 11:00:48 -0500 Subject: [PATCH 6/6] Added class doc comment and LICENSE file --- LICENSE.md | 22 ++++++++++++++++++++++ classes/kohana/kostache.php | 12 +++++++++++- classes/kohana/kostache/layout.php | 12 +++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..2118430 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,22 @@ +The MIT License + +Copyright (c) 2010-2011 Jeremy Bush +Copyright (c) 2011 Woody Gilk + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/classes/kohana/kostache.php b/classes/kohana/kostache.php index 8fa6bf3..1700507 100644 --- a/classes/kohana/kostache.php +++ b/classes/kohana/kostache.php @@ -1,5 +1,15 @@ + * @author Woody Gilk + * @copyright (c) 2010-2011 Jeremy Bush + * @copyright (c) 2011 Woody Gilk + * @license MIT + */ abstract class Kohana_Kostache { const VERSION = '2.0beta1'; diff --git a/classes/kohana/kostache/layout.php b/classes/kohana/kostache/layout.php index ac2dc1c..46bb758 100644 --- a/classes/kohana/kostache/layout.php +++ b/classes/kohana/kostache/layout.php @@ -1,5 +1,15 @@ + * @author Woody Gilk + * @copyright (c) 2010-2011 Jeremy Bush + * @copyright (c) 2011 Woody Gilk + * @license MIT + */ abstract class Kohana_Kostache_Layout extends Kostache { /**