|
|
@@ -14,6 +14,8 @@
|
|
|
*/
|
|
|
class Mustache {
|
|
|
|
|
|
+ const VERSION = '0.7.1';
|
|
|
+
|
|
|
/**
|
|
|
* Should this Mustache throw exceptions when it finds unexpected tags?
|
|
|
*
|
|
|
@@ -176,6 +178,9 @@ class Mustache {
|
|
|
if ($template === null) $template = $this->_template;
|
|
|
if ($partials !== null) $this->_partials = $partials;
|
|
|
|
|
|
+ $otag_orig = $this->_otag;
|
|
|
+ $ctag_orig = $this->_ctag;
|
|
|
+
|
|
|
if ($view) {
|
|
|
$this->_context = array($view);
|
|
|
} else if (empty($this->_context)) {
|
|
|
@@ -183,7 +188,12 @@ class Mustache {
|
|
|
}
|
|
|
|
|
|
$template = $this->_renderPragmas($template);
|
|
|
- return $this->_renderTemplate($template, $this->_context);
|
|
|
+ $template = $this->_renderTemplate($template, $this->_context);
|
|
|
+
|
|
|
+ $this->_otag = $otag_orig;
|
|
|
+ $this->_ctag = $ctag_orig;
|
|
|
+
|
|
|
+ return $template;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -214,13 +224,15 @@ class Mustache {
|
|
|
if ($section = $this->_findSection($template)) {
|
|
|
list($before, $type, $tag_name, $content, $after) = $section;
|
|
|
|
|
|
- $renderedContent = '';
|
|
|
+ $rendered_before = $this->_renderTags($before);
|
|
|
+
|
|
|
+ $rendered_content = '';
|
|
|
$val = $this->_getVariable($tag_name);
|
|
|
switch($type) {
|
|
|
// inverted section
|
|
|
case '^':
|
|
|
if (empty($val)) {
|
|
|
- $renderedContent = $this->_renderTemplate($content);
|
|
|
+ $rendered_content = $this->_renderTemplate($content);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -229,22 +241,22 @@ class Mustache {
|
|
|
if ($this->_varIsIterable($val)) {
|
|
|
foreach ($val as $local_context) {
|
|
|
$this->_pushContext($local_context);
|
|
|
- $renderedContent .= $this->_renderTemplate($content);
|
|
|
+ $rendered_content .= $this->_renderTemplate($content);
|
|
|
$this->_popContext();
|
|
|
}
|
|
|
} else if ($val) {
|
|
|
if (is_array($val) || is_object($val)) {
|
|
|
$this->_pushContext($val);
|
|
|
- $renderedContent = $this->_renderTemplate($content);
|
|
|
+ $rendered_content = $this->_renderTemplate($content);
|
|
|
$this->_popContext();
|
|
|
} else {
|
|
|
- $renderedContent = $this->_renderTemplate($content);
|
|
|
+ $rendered_content = $this->_renderTemplate($content);
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- return $this->_renderTags($before) . $renderedContent . $this->_renderTemplate($after);
|
|
|
+ return $rendered_before . $rendered_content . $this->_renderTemplate($after);
|
|
|
}
|
|
|
|
|
|
return $this->_renderTags($template);
|
|
|
@@ -260,7 +272,7 @@ class Mustache {
|
|
|
*/
|
|
|
protected function _prepareSectionRegEx($otag, $ctag) {
|
|
|
return sprintf(
|
|
|
- '/(?:(?<=\\n)[ \\t]*)?%s(?P<type>[%s])(?P<tag_name>.+?)%s\\n?/s',
|
|
|
+ '/(?:(?<=\\n)[ \\t]*)?%s(?:(?P<type>[%s])(?P<tag_name>.+?)|=(?P<delims>.*?)=)%s\\n?/s',
|
|
|
preg_quote($otag, '/'),
|
|
|
self::SECTION_TYPES,
|
|
|
preg_quote($ctag, '/')
|
|
|
@@ -286,6 +298,12 @@ class Mustache {
|
|
|
$section_stack = array();
|
|
|
$matches = array();
|
|
|
while (preg_match($regEx, $template, $matches, PREG_OFFSET_CAPTURE, $search_offset)) {
|
|
|
+ if (isset($matches['delims'][0])) {
|
|
|
+ list($otag, $ctag) = explode(' ', $matches['delims'][0]);
|
|
|
+ $regEx = $this->_prepareSectionRegEx($otag, $ctag);
|
|
|
+ $search_offset = $matches[0][1] + strlen($matches[0][0]);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
$match = $matches[0][0];
|
|
|
$offset = $matches[0][1];
|
|
|
@@ -475,9 +493,6 @@ class Mustache {
|
|
|
return $template;
|
|
|
}
|
|
|
|
|
|
- $otag_orig = $this->_otag;
|
|
|
- $ctag_orig = $this->_ctag;
|
|
|
-
|
|
|
$first = true;
|
|
|
$this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag, true);
|
|
|
|
|
|
@@ -517,9 +532,6 @@ class Mustache {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $this->_otag = $otag_orig;
|
|
|
- $this->_ctag = $ctag_orig;
|
|
|
-
|
|
|
return $html . $template;
|
|
|
}
|
|
|
|