diff --git a/Mustache.php b/Mustache.php index d8d42f6..806e76a 100644 --- a/Mustache.php +++ b/Mustache.php @@ -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)) { diff --git a/TraversableMustache.php b/TraversableMustache.php deleted file mode 100644 index 79dfc8e..0000000 --- a/TraversableMustache.php +++ /dev/null @@ -1,51 +0,0 @@ - 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; - } -} \ No newline at end of file