Made dot-notation pragma toggleable, added test cases.
This commit is contained in:
+23
-2
@@ -25,6 +25,8 @@ class Mustache {
|
|||||||
// Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8.
|
// Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8.
|
||||||
protected $charset = 'UTF-8';
|
protected $charset = 'UTF-8';
|
||||||
|
|
||||||
|
const PRAGMA_DOT_NOTATION = 'DOT-NOTATION';
|
||||||
|
|
||||||
protected $tagRegEx;
|
protected $tagRegEx;
|
||||||
|
|
||||||
protected $template = '';
|
protected $template = '';
|
||||||
@@ -32,7 +34,9 @@ class Mustache {
|
|||||||
protected $partials = array();
|
protected $partials = array();
|
||||||
protected $pragmas = array();
|
protected $pragmas = array();
|
||||||
|
|
||||||
protected $pragmasImplemented = array('DOT-NOTATION');
|
protected $pragmasImplemented = array(
|
||||||
|
self::PRAGMA_DOT_NOTATION
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mustache class constructor.
|
* Mustache class constructor.
|
||||||
@@ -220,6 +224,20 @@ class Mustache {
|
|||||||
return '';
|
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.
|
* Loop through and render individual Mustache tags.
|
||||||
*
|
*
|
||||||
@@ -408,6 +426,7 @@ class Mustache {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getVariable($tag_name, &$context) {
|
protected function getVariable($tag_name, &$context) {
|
||||||
|
if ($this->hasPragma(self::PRAGMA_DOT_NOTATION)) {
|
||||||
$chunks = explode('.', $tag_name);
|
$chunks = explode('.', $tag_name);
|
||||||
$first = array_shift($chunks);
|
$first = array_shift($chunks);
|
||||||
|
|
||||||
@@ -417,8 +436,10 @@ class Mustache {
|
|||||||
$c = array($ret);
|
$c = array($ret);
|
||||||
$ret = $this->_getVariable($next, $c);
|
$ret = $this->_getVariable($next, $c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $ret;
|
return $ret;
|
||||||
|
} else {
|
||||||
|
return $this->_getVariable($tag_name, $context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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