adding set() and bind() for method chaining (not used as often as in regular kohana views but still used often enough to be useful)
This commit is contained in:
parent
b6311e8367
commit
27659eb813
|
@ -33,4 +33,56 @@ class Kostache extends Mustache
|
|||
else
|
||||
throw new Kohana_Exception('Template file not found: templates/'.$view_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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue