merging with synapsestudios / kohana-kostache
This commit is contained in:
commit
101869e9b2
126
classes/kohana/kostache.php
Normal file
126
classes/kohana/kostache.php
Normal file
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
|
||||
class Kohana_Kostache extends Mustache
|
||||
{
|
||||
/**
|
||||
* KOstache class factory constructor.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
public static function factory($path, $template = null, $view = null, $partials = null)
|
||||
{
|
||||
$class = 'View_'.str_replace('/', '_', $path);
|
||||
|
||||
if ( ! class_exists($class))
|
||||
throw new Kohana_View_Exception('Missing Kostache View Class for ":class"', array(':class' => $class));
|
||||
|
||||
return new $class($template, $view, $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
|
||||
*/
|
||||
public function __construct($template = null, $view = null, $partials = null)
|
||||
{
|
||||
parent::__construct($template, $view, $partials);
|
||||
|
||||
$this->_charset = Kohana::$charset;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
$template = Kohana::find_file('templates', $view_location, 'mustache');
|
||||
|
||||
if ($template)
|
||||
$this->_template = file_get_contents($template);
|
||||
else
|
||||
throw new Kohana_Exception('Template file not found: templates/'.$view_location);
|
||||
|
||||
// Convert partials to expanded template strings
|
||||
foreach ($this->_partials as $key => $partial_template)
|
||||
{
|
||||
if ($location = Kohana::find_file('templates', $partial_template, 'mustache'))
|
||||
{
|
||||
$this->_partials[$key] = file_get_contents($location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a variable by name.
|
||||
*
|
||||
* // This value can be accessed as {{foo}} within the template
|
||||
* $view->set('foo', 'my value');
|
||||
*
|
||||
* You can also use an array to set several values at once:
|
||||
*
|
||||
* // Create the values {{food}} and {{beverage}} in the template
|
||||
* $view->set(array('food' => 'bread', 'beverage' => 'water'));
|
||||
*
|
||||
* @param string variable name or an array of variables
|
||||
* @param mixed value
|
||||
* @return $this
|
||||
*/
|
||||
public function set($key, $value = NULL)
|
||||
{
|
||||
if (is_array($key))
|
||||
{
|
||||
foreach ($key as $name => $value)
|
||||
{
|
||||
$this->{$name} = $value;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a value by reference. The benefit of binding is that values can
|
||||
* be altered without re-setting them. It is also possible to bind variables
|
||||
* before they have values. Assigned values will be available as a
|
||||
* variable within the template file:
|
||||
*
|
||||
* // This reference can be accessed as {{ref}} within the template
|
||||
* $view->bind('ref', $bar);
|
||||
*
|
||||
* @param string variable name
|
||||
* @param mixed referenced variable
|
||||
* @return $this
|
||||
*/
|
||||
public function bind($key, & $value)
|
||||
{
|
||||
$this->{$key} =& $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,144 +1,2 @@
|
|||
<?php
|
||||
|
||||
class Kostache extends Mustache
|
||||
{
|
||||
/**
|
||||
* KOstache class factory constructor.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
public static function factory($path, $template = null, $view = null, $partials = null)
|
||||
{
|
||||
$class = 'View_'.str_replace('/', '_', $path);
|
||||
|
||||
if ( ! class_exists($class))
|
||||
throw new Kohana_View_Exception('Missing Kostache View Class for ":class"', array(':class' => $class));
|
||||
|
||||
return new $class($template, $view, $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
|
||||
*/
|
||||
public function __construct($template = null, $view = null, $partials = null)
|
||||
{
|
||||
parent::__construct($template, $view, $partials);
|
||||
|
||||
$this->_charset = Kohana::$charset;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
$template = Kohana::find_file('templates', $view_location, 'mustache');
|
||||
|
||||
if ($template)
|
||||
$this->_template = file_get_contents($template);
|
||||
else
|
||||
throw new Kohana_Exception('Template file not found: templates/'.$view_location);
|
||||
|
||||
// Convert partials to expanded template strings
|
||||
foreach ($this->_partials as $key => $partial_template)
|
||||
{
|
||||
if ($location = Kohana::find_file('templates', $partial_template, 'mustache'))
|
||||
{
|
||||
$this->_partials[$key] = file_get_contents($location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a variable by name.
|
||||
*
|
||||
* // This value can be accessed as {{foo}} within the template
|
||||
* $view->set('foo', 'my value');
|
||||
*
|
||||
* You can also use an array to set several values at once:
|
||||
*
|
||||
* // Create the values {{food}} and {{beverage}} in the template
|
||||
* $view->set(array('food' => 'bread', 'beverage' => 'water'));
|
||||
*
|
||||
* @param string variable name or an array of variables
|
||||
* @param mixed value
|
||||
* @return $this
|
||||
*/
|
||||
public function set($key, $value = NULL)
|
||||
{
|
||||
if (is_array($key))
|
||||
{
|
||||
foreach ($key as $name => $value)
|
||||
{
|
||||
$this->{$name} = $value;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a value by reference. The benefit of binding is that values can
|
||||
* be altered without re-setting them. It is also possible to bind variables
|
||||
* before they have values. Assigned values will be available as a
|
||||
* variable within the template file:
|
||||
*
|
||||
* // This reference can be accessed as {{ref}} within the template
|
||||
* $view->bind('ref', $bar);
|
||||
*
|
||||
* @param string variable name
|
||||
* @param mixed referenced variable
|
||||
* @return $this
|
||||
*/
|
||||
public function bind($key, & $value)
|
||||
{
|
||||
$this->{$key} =& $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overriding __toString to use Kohana's exception handler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
try
|
||||
{
|
||||
$result = $this->render();
|
||||
return $result;
|
||||
}
|
||||
catch (Exception $e) {
|
||||
Kohana::exception_handler($e);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
class Kostache extends Kohana_Kostache {}
|
Loading…
Reference in a new issue