Kostache not extending Mustache, aka Kostache v2.0beta1
This commit is contained in:
parent
21ec6521a4
commit
e77f6a99a2
|
@ -1,51 +1,121 @@
|
||||||
<?php
|
<?php defined('SYSPATH') or die('No direct script access.');
|
||||||
|
|
||||||
class Kohana_Kostache extends Mustache
|
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
|
* @param string template path
|
||||||
* array of partials as well.
|
* @param array partial paths
|
||||||
*
|
* @return Kostache
|
||||||
* @access public
|
* @throws Kohana_Exception if the view class does not exist
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
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))
|
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.
|
* @var string Mustache template
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*/
|
*/
|
||||||
public function __construct($template = null, $view = null, $partials = null)
|
protected $_template;
|
||||||
{
|
|
||||||
parent::__construct($template, $view, $partials);
|
|
||||||
|
|
||||||
$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
|
* @return string
|
||||||
* @uses View::render
|
|
||||||
*/
|
*/
|
||||||
public function __toString()
|
public function render()
|
||||||
{
|
{
|
||||||
try
|
return $this->_stash($this->_template, $this, $this->_partials)->render();
|
||||||
{
|
|
||||||
return $this->render();
|
|
||||||
}
|
|
||||||
catch (Exception $e)
|
|
||||||
{
|
|
||||||
// Display the exception message
|
|
||||||
Kohana::exception_handler($e);
|
|
||||||
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
return new Mustache($template, $view, $partials);
|
||||||
{
|
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
31
classes/kohana/kostache/layout.php
Normal file
31
classes/kohana/kostache/layout.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php defined('SYSPATH') or die('No direct script access.');
|
||||||
|
|
||||||
|
class Kohana_Kostache_Layout extends Kostache {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string partial name for content
|
||||||
|
*/
|
||||||
|
const CONTENT_PARTIAL = 'content';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string layout path
|
||||||
|
*/
|
||||||
|
protected $_layout = 'layout';
|
||||||
|
|
||||||
|
public function render()
|
||||||
|
{
|
||||||
|
if ( ! $this->_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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,2 +1,3 @@
|
||||||
<?php
|
<?php defined('SYSPATH') or die('No direct script access.');
|
||||||
class Kostache extends Kohana_Kostache {}
|
|
||||||
|
class Kostache extends Kohana_Kostache { }
|
||||||
|
|
3
classes/kostache/layout.php
Normal file
3
classes/kostache/layout.php
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
<?php defined('SYSPATH') or die('No direct script access.');
|
||||||
|
|
||||||
|
class Kostache_Layout extends Kohana_Kostache_Layout { }
|
|
@ -1,40 +0,0 @@
|
||||||
<?php defined('SYSPATH') or die('No direct script access.');
|
|
||||||
|
|
||||||
class View_Kohana_Layout extends Kostache
|
|
||||||
{
|
|
||||||
protected $_layout = 'layout';
|
|
||||||
|
|
||||||
public $render_layout = TRUE;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string template title
|
|
||||||
*/
|
|
||||||
public $title = 'Brought to you by Kostache!';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Renders the body template into the layout
|
|
||||||
*/
|
|
||||||
public function render($template = null, $view = null, $partials = null)
|
|
||||||
{
|
|
||||||
// Override the template location to match kohana's conventions
|
|
||||||
if ( ! $this->_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
|
|
|
@ -1,6 +0,0 @@
|
||||||
<?php defined('SYSPATH') or die('No direct script access.');
|
|
||||||
|
|
||||||
class View_Layout extends View_Kohana_Layout
|
|
||||||
{
|
|
||||||
|
|
||||||
} // End View_Layout
|
|
Loading…
Reference in a new issue