Made dot-notation pragma toggleable, added test cases.
This commit is contained in:
+31
-10
@@ -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.
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
require_once '../Mustache.php';
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
|
||||
class MustachePragmaDotNotationTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testDotTraversal() {
|
||||
$m = new Mustache('', array('foo' => 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');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user