Merge branch 'dev' into feature/higher-order-sections
Conflicts: Mustache.php
This commit is contained in:
+41
-39
@@ -14,6 +14,9 @@
|
|||||||
*/
|
*/
|
||||||
class Mustache {
|
class Mustache {
|
||||||
|
|
||||||
|
const VERSION = '0.7.1';
|
||||||
|
const SPEC_VERSION = '1.1.2';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should this Mustache throw exceptions when it finds unexpected tags?
|
* Should this Mustache throw exceptions when it finds unexpected tags?
|
||||||
*
|
*
|
||||||
@@ -176,6 +179,9 @@ class Mustache {
|
|||||||
if ($template === null) $template = $this->_template;
|
if ($template === null) $template = $this->_template;
|
||||||
if ($partials !== null) $this->_partials = $partials;
|
if ($partials !== null) $this->_partials = $partials;
|
||||||
|
|
||||||
|
$otag_orig = $this->_otag;
|
||||||
|
$ctag_orig = $this->_ctag;
|
||||||
|
|
||||||
if ($view) {
|
if ($view) {
|
||||||
$this->_context = array($view);
|
$this->_context = array($view);
|
||||||
} else if (empty($this->_context)) {
|
} else if (empty($this->_context)) {
|
||||||
@@ -183,7 +189,12 @@ class Mustache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$template = $this->_renderPragmas($template);
|
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -211,28 +222,18 @@ class Mustache {
|
|||||||
* @return string Rendered Mustache template.
|
* @return string Rendered Mustache template.
|
||||||
*/
|
*/
|
||||||
protected function _renderTemplate($template) {
|
protected function _renderTemplate($template) {
|
||||||
$template = $this->_renderSections($template);
|
if ($section = $this->_findSection($template)) {
|
||||||
return $this->_renderTags($template);
|
list($before, $type, $tag_name, $content, $after) = $section;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$rendered_before = $this->_renderTags($before);
|
||||||
* Render boolean, enumerable and inverted sections.
|
|
||||||
*
|
|
||||||
* @access protected
|
|
||||||
* @param string $template
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function _renderSections($template) {
|
|
||||||
while ($section_data = $this->_findSection($template)) {
|
|
||||||
list($section, $offset, $type, $tag_name, $content) = $section_data;
|
|
||||||
|
|
||||||
$replace = '';
|
$rendered_content = '';
|
||||||
$val = $this->_getVariable($tag_name);
|
$val = $this->_getVariable($tag_name);
|
||||||
switch($type) {
|
switch($type) {
|
||||||
// inverted section
|
// inverted section
|
||||||
case '^':
|
case '^':
|
||||||
if (empty($val)) {
|
if (empty($val)) {
|
||||||
$replace .= $content;
|
$rendered_content = $this->_renderTemplate($content);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -240,30 +241,29 @@ class Mustache {
|
|||||||
case '#':
|
case '#':
|
||||||
// higher order sections
|
// higher order sections
|
||||||
if ($this->_varIsCallable($val)) {
|
if ($this->_varIsCallable($val)) {
|
||||||
$content = call_user_func($val, $content);
|
$rendered_content = $this->_renderTemplate(call_user_func($val, $content));
|
||||||
$replace .= $this->_renderTemplate($content);
|
|
||||||
} else if ($this->_varIsIterable($val)) {
|
} else if ($this->_varIsIterable($val)) {
|
||||||
foreach ($val as $local_context) {
|
foreach ($val as $local_context) {
|
||||||
$this->_pushContext($local_context);
|
$this->_pushContext($local_context);
|
||||||
$replace .= $this->_renderTemplate($content);
|
$rendered_content .= $this->_renderTemplate($content);
|
||||||
$this->_popContext();
|
$this->_popContext();
|
||||||
}
|
}
|
||||||
} else if ($val) {
|
} else if ($val) {
|
||||||
if (is_array($val) || is_object($val)) {
|
if (is_array($val) || is_object($val)) {
|
||||||
$this->_pushContext($val);
|
$this->_pushContext($val);
|
||||||
$replace .= $this->_renderTemplate($content);
|
$rendered_content = $this->_renderTemplate($content);
|
||||||
$this->_popContext();
|
$this->_popContext();
|
||||||
} else {
|
} else {
|
||||||
$replace .= $content;
|
$rendered_content = $this->_renderTemplate($content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$template = substr_replace($template, $replace, $offset, strlen($section));
|
return $rendered_before . $rendered_content . $this->_renderTemplate($after);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $template;
|
return $this->_renderTags($template);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -276,7 +276,7 @@ class Mustache {
|
|||||||
*/
|
*/
|
||||||
protected function _prepareSectionRegEx($otag, $ctag) {
|
protected function _prepareSectionRegEx($otag, $ctag) {
|
||||||
return sprintf(
|
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, '/'),
|
preg_quote($otag, '/'),
|
||||||
self::SECTION_TYPES,
|
self::SECTION_TYPES,
|
||||||
preg_quote($ctag, '/')
|
preg_quote($ctag, '/')
|
||||||
@@ -284,13 +284,11 @@ class Mustache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract a section from $template.
|
* Extract the first section from $template.
|
||||||
*
|
|
||||||
* This is a helper function to find sections needed by _renderSections.
|
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $template
|
* @param string $template
|
||||||
* @return array $section, $offset, $type, $tag_name and $content
|
* @return array $before, $type, $tag_name, $content and $after
|
||||||
*/
|
*/
|
||||||
protected function _findSection($template) {
|
protected function _findSection($template) {
|
||||||
$regEx = $this->_prepareSectionRegEx($this->_otag, $this->_ctag);
|
$regEx = $this->_prepareSectionRegEx($this->_otag, $this->_ctag);
|
||||||
@@ -304,6 +302,12 @@ class Mustache {
|
|||||||
$section_stack = array();
|
$section_stack = array();
|
||||||
$matches = array();
|
$matches = array();
|
||||||
while (preg_match($regEx, $template, $matches, PREG_OFFSET_CAPTURE, $search_offset)) {
|
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];
|
$match = $matches[0][0];
|
||||||
$offset = $matches[0][1];
|
$offset = $matches[0][1];
|
||||||
@@ -330,10 +334,14 @@ class Mustache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (empty($section_stack)) {
|
if (empty($section_stack)) {
|
||||||
$section = substr($template, $section_start, $search_offset - $section_start);
|
// $before, $type, $tag_name, $content, $after
|
||||||
$content = substr($template, $content_start, $offset - $content_start);
|
return array(
|
||||||
|
substr($template, 0, $section_start),
|
||||||
return array($section, $section_start, $section_type, $tag_name, $content);
|
$section_type,
|
||||||
|
$tag_name,
|
||||||
|
substr($template, $content_start, $offset - $content_start),
|
||||||
|
substr($template, $search_offset),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -489,9 +497,6 @@ class Mustache {
|
|||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
$otag_orig = $this->_otag;
|
|
||||||
$ctag_orig = $this->_ctag;
|
|
||||||
|
|
||||||
$first = true;
|
$first = true;
|
||||||
$this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag, true);
|
$this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag, true);
|
||||||
|
|
||||||
@@ -531,9 +536,6 @@ class Mustache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_otag = $otag_orig;
|
|
||||||
$this->_ctag = $ctag_orig;
|
|
||||||
|
|
||||||
return $html . $template;
|
return $html . $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -578,7 +580,7 @@ class Mustache {
|
|||||||
case '#':
|
case '#':
|
||||||
case '^':
|
case '^':
|
||||||
case '/':
|
case '/':
|
||||||
// remove any leftovers from _renderSections
|
// remove any leftover section tags
|
||||||
return $leading . $trailing;
|
return $leading . $trailing;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
+3
-6
@@ -81,15 +81,12 @@ And render it:
|
|||||||
Known Issues
|
Known Issues
|
||||||
------------
|
------------
|
||||||
|
|
||||||
* As of v1.1.2, there are a couple of whitespace bugs around section tags.
|
* As of Mustache spec v1.1.2, there are a couple of whitespace bugs around section tags... Despite these failing tests, this
|
||||||
* Things get weird when you change delimiters inside a section -- `delimiters` example currently fails with an
|
version is actually *closer* to correct than previous releases.
|
||||||
"unclosed section" exception.
|
|
||||||
* The current spec test exposes several whitespace bugs (which are mostly instances of the exact same whitespace
|
|
||||||
bug) ... Despite these failing tests, this version is actually *closer* to correct than previous releases.
|
|
||||||
|
|
||||||
|
|
||||||
See Also
|
See Also
|
||||||
--------
|
--------
|
||||||
|
|
||||||
* [Readme for the Ruby Mustache implementation](http://github.com/defunkt/mustache/blob/master/README.md).
|
* [Readme for the Ruby Mustache implementation](http://github.com/defunkt/mustache/blob/master/README.md).
|
||||||
* [mustache(1)](http://defunkt.github.com/mustache/mustache.1.html) and [mustache(5)](http://defunkt.github.com/mustache/mustache.5.html) man pages.
|
* [mustache(1)](http://mustache.github.com/mustache.1.html) and [mustache(5)](http://mustache.github.com/mustache.5.html) man pages.
|
||||||
|
|||||||
+37
-2
@@ -36,7 +36,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
|
|||||||
const TEST_CLASS = 'Mustache';
|
const TEST_CLASS = 'Mustache';
|
||||||
|
|
||||||
protected $knownIssues = array(
|
protected $knownIssues = array(
|
||||||
'Delimiters' => "Known issue: sections don't respect delimiter changes",
|
// Just the whitespace ones...
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -195,6 +195,30 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
|
|||||||
$this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
|
$this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin')));
|
||||||
$this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
|
$this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group interpolation
|
||||||
|
* @dataProvider interpolationData
|
||||||
|
*/
|
||||||
|
public function testDoubleRenderMustacheTags($template, $context, $expected) {
|
||||||
|
$m = new Mustache($template, $context);
|
||||||
|
$this->assertEquals($expected, $m->render());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function interpolationData() {
|
||||||
|
return array(
|
||||||
|
array(
|
||||||
|
'{{#a}}{{=<% %>=}}{{b}} c<%={{ }}=%>{{/a}}',
|
||||||
|
array('a' => array(array('b' => 'Do Not Render'))),
|
||||||
|
'{{b}} c'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'{{#a}}{{b}}{{/a}}',
|
||||||
|
array('a' => array('b' => '{{c}}'), 'c' => 'FAIL'),
|
||||||
|
'{{c}}'
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mustache should allow newlines (and other whitespace) in comments and all other tags.
|
* Mustache should allow newlines (and other whitespace) in comments and all other tags.
|
||||||
@@ -378,6 +402,17 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
|
|||||||
$this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>'));
|
$this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group delimiters
|
||||||
|
*/
|
||||||
|
public function testStickyDelimiters() {
|
||||||
|
$m = new Mustache(null, array('result' => 'FAIL'));
|
||||||
|
$this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{ result }}[[={{ }}=]]'));
|
||||||
|
$this->assertEquals('{{#result}}{{/result}}', $m->render('{{=[[ ]]=}}{{#result}}{{/result}}[[={{ }}=]]'));
|
||||||
|
$this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}[[#result]]{{ result }}[[/result]][[={{ }}=]]'));
|
||||||
|
$this->assertEquals('{{ result }}', $m->render('{{#result}}{{=[[ ]]=}}{{ result }}[[/result]][[^result]][[={{ }}=]][[ result ]]{{/result}}'));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group sections
|
* @group sections
|
||||||
* @dataProvider poorlyNestedSections
|
* @dataProvider poorlyNestedSections
|
||||||
@@ -426,4 +461,4 @@ class MustacheExposedOptionsStub extends Mustache {
|
|||||||
public function getDelimiters() {
|
public function getDelimiters() {
|
||||||
return array($this->_otag, $this->_ctag);
|
return array($this->_otag, $this->_ctag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user