|
@@ -38,38 +38,6 @@ class Mustache {
|
|
|
* will change in the future.
|
|
* 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}}}
|
|
* 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
|
|
* 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 $_pragmas = array();
|
|
|
|
|
|
|
|
protected $_pragmasImplemented = array(
|
|
protected $_pragmasImplemented = array(
|
|
|
- self::PRAGMA_DOT_NOTATION,
|
|
|
|
|
- self::PRAGMA_IMPLICIT_ITERATOR,
|
|
|
|
|
self::PRAGMA_UNESCAPED
|
|
self::PRAGMA_UNESCAPED
|
|
|
);
|
|
);
|
|
|
|
|
|
|
@@ -128,9 +94,7 @@ class Mustache {
|
|
|
*
|
|
*
|
|
|
* // an array of pragmas to enable
|
|
* // an array of pragmas to enable
|
|
|
* 'pragmas' => array(
|
|
* 'pragmas' => array(
|
|
|
- * Mustache::PRAGMA_UNESCAPED,
|
|
|
|
|
- * Mustache::PRAGMA_DOT_NOTATION,
|
|
|
|
|
- * Mustache::PRAGMA_IMPLICIT_ITERATOR
|
|
|
|
|
|
|
+ * Mustache::PRAGMA_UNESCAPED
|
|
|
* ),
|
|
* ),
|
|
|
* );
|
|
* );
|
|
|
*
|
|
*
|
|
@@ -279,30 +243,13 @@ class Mustache {
|
|
|
|
|
|
|
|
// regular section
|
|
// regular section
|
|
|
case '#':
|
|
case '#':
|
|
|
-
|
|
|
|
|
// higher order sections
|
|
// higher order sections
|
|
|
if ($this->_varIsCallable($val)) {
|
|
if ($this->_varIsCallable($val)) {
|
|
|
$content = call_user_func($val, $content);
|
|
$content = call_user_func($val, $content);
|
|
|
$replace .= $this->_renderTemplate($content);
|
|
$replace .= $this->_renderTemplate($content);
|
|
|
} else if ($this->_varIsIterable($val)) {
|
|
} else 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) {
|
|
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);
|
|
$replace .= $this->_renderTemplate($content);
|
|
|
$this->_popContext();
|
|
$this->_popContext();
|
|
|
}
|
|
}
|
|
@@ -685,8 +632,10 @@ class Mustache {
|
|
|
*/
|
|
*/
|
|
|
protected function _renderPartial($tag_name, $whitespace = '') {
|
|
protected function _renderPartial($tag_name, $whitespace = '') {
|
|
|
$view = clone($this);
|
|
$view = clone($this);
|
|
|
-
|
|
|
|
|
- return $whitespace . preg_replace('/\n(?!$)/s', "\n" . $whitespace, $view->render($this->_getPartial($tag_name)));
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $partial = $whitespace . preg_replace('/\n(?!$)/s', "\n" . $whitespace, $this->_getPartial($tag_name));
|
|
|
|
|
+
|
|
|
|
|
+ return $view->render($partial);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -755,7 +704,9 @@ class Mustache {
|
|
|
* @return string
|
|
* @return string
|
|
|
*/
|
|
*/
|
|
|
protected function _getVariable($tag_name) {
|
|
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);
|
|
$chunks = explode('.', $tag_name);
|
|
|
$first = array_shift($chunks);
|
|
$first = array_shift($chunks);
|
|
|
|
|
|