DOT-NOTATION and IMPLICIT-ITERATOR are now part of core spec.

These pragmas have proved useful enough that they're no longer considered pragmas. Enable them by default. Update tests accordingly.

This is a (very) backwards incompatible change, so it won't be released until v0.6.0.
This commit is contained in:
Justin Hileman
2011-03-05 11:49:43 -05:00
parent f0861da000
commit 25253b3c00
7 changed files with 15 additions and 217 deletions
+5 -55
View File
@@ -38,38 +38,6 @@ class Mustache {
* will change in the future.
*/
/**
* The {{%DOT-NOTATION}} pragma allows context traversal via dots. Given the following context:
*
* $context = array('foo' => array('bar' => array('baz' => 'qux')));
*
* One could access nested properties using dot notation:
*
* {{%DOT-NOTATION}}{{foo.bar.baz}}
*
* Which would render as `qux`.
*/
const PRAGMA_DOT_NOTATION = 'DOT-NOTATION';
/**
* The {{%IMPLICIT-ITERATOR}} pragma allows access to non-associative array data in an
* iterable section:
*
* $context = array('items' => array('foo', 'bar', 'baz'));
*
* With this template:
*
* {{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}
*
* Would render as `foobarbaz`.
*
* {{%IMPLICIT-ITERATOR}} accepts an optional 'iterator' argument which allows implicit
* iterator tags other than {{.}} ...
*
* {{%IMPLICIT-ITERATOR iterator=i}}{{#items}}{{i}}{{/items}}
*/
const PRAGMA_IMPLICIT_ITERATOR = 'IMPLICIT-ITERATOR';
/**
* The {{%UNESCAPED}} pragma swaps the meaning of the {{normal}} and {{{unescaped}}}
* Mustache tags. That is, once this pragma is activated the {{normal}} tag will not be
@@ -99,8 +67,6 @@ class Mustache {
protected $_pragmas = array();
protected $_pragmasImplemented = array(
self::PRAGMA_DOT_NOTATION,
self::PRAGMA_IMPLICIT_ITERATOR,
self::PRAGMA_UNESCAPED
);
@@ -123,9 +89,7 @@ class Mustache {
*
* // an array of pragmas to enable
* 'pragmas' => array(
* Mustache::PRAGMA_UNESCAPED,
* Mustache::PRAGMA_DOT_NOTATION,
* Mustache::PRAGMA_IMPLICIT_ITERATOR
* Mustache::PRAGMA_UNESCAPED
* ),
* );
*
@@ -275,24 +239,8 @@ class Mustache {
// regular section
case '#':
if ($this->_varIsIterable($val)) {
if ($this->_hasPragma(self::PRAGMA_IMPLICIT_ITERATOR)) {
if ($opt = $this->_getPragmaOptions(self::PRAGMA_IMPLICIT_ITERATOR)) {
$iterator = $opt['iterator'];
} else {
$iterator = '.';
}
} else {
$iterator = false;
}
foreach ($val as $local_context) {
if ($iterator) {
$iterator_context = array($iterator => $local_context);
$this->_pushContext($iterator_context);
} else {
$this->_pushContext($local_context);
}
$this->_pushContext($local_context);
$replace .= $this->_renderTemplate($content);
$this->_popContext();
}
@@ -737,7 +685,9 @@ class Mustache {
* @return string
*/
protected function _getVariable($tag_name) {
if ($tag_name != '.' && strpos($tag_name, '.') !== false && $this->_hasPragma(self::PRAGMA_DOT_NOTATION)) {
if ($tag_name === '.') {
return $this->_context[0];
} else if (strpos($tag_name, '.') !== false) {
$chunks = explode('.', $tag_name);
$first = array_shift($chunks);