Merge branch 'feature/inverted-sections' into dev

This commit is contained in:
Justin Hileman
2010-04-26 19:03:09 -04:00
7 changed files with 34 additions and 19 deletions
+18 -5
View File
@@ -106,7 +106,7 @@ class Mustache {
}
/**
* Render boolean and enumerable sections.
* Render boolean, enumerable and inverted sections.
*
* @access protected
* @param string $template
@@ -120,17 +120,28 @@ class Mustache {
$otag = $this->prepareRegEx($this->otag);
$ctag = $this->prepareRegEx($this->ctag);
$regex = '/' . $otag . '\\#(.+?)' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\1' . $ctag . '\\s*/m';
$regex = '/' . $otag . '(\\^|\\#)(.+?)' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\2' . $ctag . '\\s*/m';
$matches = array();
while (preg_match($regex, $template, $matches, PREG_OFFSET_CAPTURE)) {
$section = $matches[0][0];
$offset = $matches[0][1];
$tag_name = trim($matches[1][0]);
$content = $matches[2][0];
$type = $matches[1][0];
$tag_name = trim($matches[2][0]);
$content = $matches[3][0];
$replace = '';
$val = $this->getVariable($tag_name, $context);
switch($type) {
// inverted section
case '^':
if (empty($val)) {
$replace .= $content;
}
break;
// regular section
case '#':
if (is_array($val)) {
foreach ($val as $local_context) {
$replace .= $this->_render($content, $this->getContext($context, $local_context));
@@ -138,6 +149,8 @@ class Mustache {
} else if ($val) {
$replace .= $content;
}
break;
}
$template = substr_replace($template, $replace, $offset, strlen($section));
}
@@ -293,7 +306,7 @@ class Mustache {
$otag = $this->prepareRegEx($this->otag);
$ctag = $this->prepareRegEx($this->ctag);
$this->tagRegEx = '/' . $otag . "(#|\/|=|!|>|\\{|&)?([^\/#]+?)\\1?" . $ctag . "+/";
$this->tagRegEx = '/' . $otag . "(#|\/|=|!|>|\\{|&)?([^\/#\^]+?)\\1?" . $ctag . "+/";
return '';
}
-1
View File
@@ -82,7 +82,6 @@ Known Issues
------------
* Sections don't respect delimiter changes -- `delimiters` example currently fails with an "unclosed section" exception.
* Since `complex` example emulates some fancy swizzling available in Ruby, it fails. Need to convert example to PHPisms (In PHP, Mustache class doesn't maintain current context stack -- available context is passed to methods via params).
* Test coverage is incomplete.
+2 -2
View File
@@ -5,9 +5,9 @@
{{#current}}
<li><strong>{{name}}</strong></li>
{{/current}}
{{#isLink}}
{{^current}}
<li><a href="{{url}}">{{name}}</a></li>
{{/isLink}}
{{/current}}
{{/item}}
</ul>
{{/notEmpty}}
-5
View File
@@ -9,11 +9,6 @@ class Complex extends Mustache {
array('name' => 'blue', 'current' => false, 'url' => '#Blue'),
);
public function isLink() {
// Exploit the fact that the current iteration item is at the top of the context stack.
return $this->getVariable('current', $this->context) != true;
}
public function notEmpty() {
return !($this->isEmpty());
}
@@ -0,0 +1,5 @@
<?php
class InvertedSection extends Mustache {
public $repo = array();
}
@@ -0,0 +1,2 @@
{{#repo}}<b>{{name}}</b>{{/repo}}
{{^repo}}No repos :({{/repo}}
@@ -0,0 +1 @@
No repos :(