diff --git a/Mustache.php b/Mustache.php index d30de53..e47e532 100644 --- a/Mustache.php +++ b/Mustache.php @@ -25,6 +25,8 @@ class Mustache { // Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8. protected $charset = 'UTF-8'; + const PRAGMA_DOT_NOTATION = 'DOT-NOTATION'; + protected $tagRegEx; protected $template = ''; @@ -32,7 +34,9 @@ class Mustache { protected $partials = array(); protected $pragmas = array(); - protected $pragmasImplemented = array('DOT-NOTATION'); + protected $pragmasImplemented = array( + self::PRAGMA_DOT_NOTATION + ); /** * Mustache class constructor. @@ -168,7 +172,7 @@ class Mustache { /** * Initialize pragmas and remove all pragma tags. - * + * * @access protected * @param string $template * @param array &$context @@ -220,6 +224,20 @@ class Mustache { return ''; } + protected function hasPragma($pragma_name) { + if (array_key_exists($pragma_name, $this->pragmas) && $this->pragmas[$pragma_name]) { + return true; + } + } + + protected function getPragmaOptions($pragma_name) { + if (!$this->hasPragma()) { + throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA); + } + + return $this->pragmas[$pragma_name]; + } + /** * Loop through and render individual Mustache tags. * @@ -408,17 +426,20 @@ class Mustache { * @return string */ protected function getVariable($tag_name, &$context) { - $chunks = explode('.', $tag_name); - $first = array_shift($chunks); + if ($this->hasPragma(self::PRAGMA_DOT_NOTATION)) { + $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); + $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; + } else { + return $this->_getVariable($tag_name, $context); } - - return $ret; } /** diff --git a/test/MustachePragmaDotNotationTest.php b/test/MustachePragmaDotNotationTest.php new file mode 100644 index 0000000..d6d31b7 --- /dev/null +++ b/test/MustachePragmaDotNotationTest.php @@ -0,0 +1,34 @@ + array('bar' => 'this worked'))); + + $this->assertEquals($m->render('{{foo.bar}}'), '', + 'Dot notation not enabled, variable should have been replaced with nothing'); + $this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar}}'), 'this worked', + 'Dot notation enabled, variable should have been replaced by "this worked"'); + } + + public function testDeepTraversal() { + $data = array( + 'foo' => array('bar' => array('baz' => array('qux' => array('quux' => 'WIN!')))), + 'a' => array('b' => array('c' => array('d' => array('e' => 'abcs')))), + 'one' => array( + 'one' => 'one-one', + 'two' => 'one-two', + 'three' => 'one-three', + ), + ); + + $m = new Mustache('', $data); + $this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar.baz.qux.quux}}'), 'WIN!'); + $this->assertEquals($m->render('{{%DOT-NOTATION}}{{a.b.c.d.e}}'), 'abcs'); + $this->assertEquals($m->render('{{%DOT-NOTATION}}{{one.one}}|{{one.two}}|{{one.three}}'), 'one-one|one-two|one-three'); + } + +} \ No newline at end of file