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 9d4be4c..1700507 100644 --- a/classes/kohana/kostache.php +++ b/classes/kohana/kostache.php @@ -1,51 +1,140 @@ - + * @author Woody Gilk + * @copyright (c) 2010-2011 Jeremy Bush + * @copyright (c) 2011 Woody Gilk + * @license MIT + */ +abstract class Kohana_Kostache { -class Kohana_Kostache extends Mustache -{ - const VERSION = 1.3; - - protected $_partials_processed = FALSE; + const VERSION = '2.0beta1'; /** - * Kostache class factory constructor. + * Factory method for Kostache views. Accepts a template path and an + * optional array of partial paths. * - * This method accepts a $template string and a $view object. Optionally, pass an associative - * array of partials as well. - * - * @access public - * @param string $path the path of the class to load - * @param string $template (default: null) - * @param mixed $view (default: null) - * @param array $partials (default: null) - * @return void + * @param string template path + * @param array partial paths + * @return Kostache + * @throws Kohana_Exception if the view class does not exist */ - public static function factory($path, $template = null, $view = null, $partials = null) + public static function factory($template, array $partials = NULL) { - $class = 'View_'.str_replace('/', '_', $path); + $class = 'View_'.str_replace('/', '_', $template); if ( ! class_exists($class)) - throw new Kohana_View_Exception('Missing Kostache View Class for ":class"', array(':class' => $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 ($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) + { + // 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 +190,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..46bb758 --- /dev/null +++ b/classes/kohana/kostache/layout.php @@ -0,0 +1,46 @@ + + * @author Woody Gilk + * @copyright (c) 2010-2011 Jeremy Bush + * @copyright (c) 2011 Woody Gilk + * @license MIT + */ +abstract class Kohana_Kostache_Layout extends Kostache { + + /** + * @var string partial name for content + */ + const CONTENT_PARTIAL = 'content'; + + /** + * @var boolean render template in layout? + */ + public $render_layout = TRUE; + + /** + * @var string layout path + */ + protected $_layout = 'layout'; + + public function render() + { + if ( ! $this->render_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..9566dc3 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 @@ -