Merging changes from TraversableMustache

This commit is contained in:
Justin Hileman
2010-04-23 11:57:24 -04:00
parent 88f67fc53e
commit db4ff1058d
2 changed files with 25 additions and 52 deletions
+25 -1
View File
@@ -24,7 +24,7 @@ class Mustache {
// Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8.
protected $charset = 'UTF-8';
protected $tagRegEx;
protected $template = '';
@@ -333,6 +333,30 @@ class Mustache {
* @return string
*/
protected function getVariable($tag_name, &$context) {
$chunks = explode('.', $tag_name);
$first = array_shift($chunks);
$ret = $this->_getVariable($first, $context);
while ($next = array_shift($chunks)) {
// Slice off a chunk of context for dot notation traversal.
$c = array($ret);
$ret = $this->_getVariable($next, $c);
}
return $ret;
}
/**
* Get a variable from the context array. Internal helper used by getVariable() to abstract
* variable traversal for dot notation.
*
* @access protected
* @param string $tag_name
* @param array &$context
* @throws MustacheException Unknown variable name.
* @return string
*/
protected function _getVariable($tag_name, &$context) {
foreach ($context as $view) {
if (is_object($view)) {
if (isset($view->$tag_name)) {
-51
View File
@@ -1,51 +0,0 @@
<?php
/**
* TraversableMustache class.
*
* This is an implementaiton of the DOT_NOTATION pragma.
*
* A Mustache subclass which allows variable traversal via dots, i.e.:
*
* @code
* class Foo extends TraversableMustache {
* var $one = array(
* 'two' => array(
* 'three' => 'wheee!'
* )
* );
*
* protected $template = '{{one.two.three}}';
* }
* $foo = new Foo;
* print $foo;
* @endcode
*
* (The above code prints 'wheee!')
*
* @extends Mustache
*/
class TraversableMustache extends Mustache {
/**
* Override default getVariable method to allow object traversal via dots.
* This might be cool. Also, might be heinous.
*
* @access protected
* @param string $tag_name
* @param array &$context
* @return string
*/
protected function getVariable($tag_name, &$context) {
$chunks = explode('.', $tag_name);
$first = array_shift($chunks);
$ret = parent::getVariable($first, $context);
while ($next = array_shift($chunks)) {
$c = array($ret);
$ret = parent::getVariable($next, $c);
}
return $ret;
}
}