Add inverted section.
This commit is contained in:
+18
-5
@@ -106,7 +106,7 @@ class Mustache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render boolean and enumerable sections.
|
* Render boolean, enumerable and inverted sections.
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param string $template
|
* @param string $template
|
||||||
@@ -120,17 +120,28 @@ class Mustache {
|
|||||||
|
|
||||||
$otag = $this->prepareRegEx($this->otag);
|
$otag = $this->prepareRegEx($this->otag);
|
||||||
$ctag = $this->prepareRegEx($this->ctag);
|
$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();
|
$matches = array();
|
||||||
while (preg_match($regex, $template, $matches, PREG_OFFSET_CAPTURE)) {
|
while (preg_match($regex, $template, $matches, PREG_OFFSET_CAPTURE)) {
|
||||||
$section = $matches[0][0];
|
$section = $matches[0][0];
|
||||||
$offset = $matches[0][1];
|
$offset = $matches[0][1];
|
||||||
$tag_name = trim($matches[1][0]);
|
$type = $matches[1][0];
|
||||||
$content = $matches[2][0];
|
$tag_name = trim($matches[2][0]);
|
||||||
|
$content = $matches[3][0];
|
||||||
|
|
||||||
$replace = '';
|
$replace = '';
|
||||||
$val = $this->getVariable($tag_name, $context);
|
$val = $this->getVariable($tag_name, $context);
|
||||||
|
switch($type) {
|
||||||
|
// inverted section
|
||||||
|
case '^':
|
||||||
|
if (empty($val)) {
|
||||||
|
$replace .= $content;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
// regular section
|
||||||
|
case '#':
|
||||||
if (is_array($val)) {
|
if (is_array($val)) {
|
||||||
foreach ($val as $local_context) {
|
foreach ($val as $local_context) {
|
||||||
$replace .= $this->_render($content, $this->getContext($context, $local_context));
|
$replace .= $this->_render($content, $this->getContext($context, $local_context));
|
||||||
@@ -138,6 +149,8 @@ class Mustache {
|
|||||||
} else if ($val) {
|
} else if ($val) {
|
||||||
$replace .= $content;
|
$replace .= $content;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$template = substr_replace($template, $replace, $offset, strlen($section));
|
$template = substr_replace($template, $replace, $offset, strlen($section));
|
||||||
}
|
}
|
||||||
@@ -293,7 +306,7 @@ class Mustache {
|
|||||||
|
|
||||||
$otag = $this->prepareRegEx($this->otag);
|
$otag = $this->prepareRegEx($this->otag);
|
||||||
$ctag = $this->prepareRegEx($this->ctag);
|
$ctag = $this->prepareRegEx($this->ctag);
|
||||||
$this->tagRegEx = '/' . $otag . "(#|\/|=|!|>|\\{|&)?([^\/#]+?)\\1?" . $ctag . "+/";
|
$this->tagRegEx = '/' . $otag . "(#|\/|=|!|>|\\{|&)?([^\/#\^]+?)\\1?" . $ctag . "+/";
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 :(
|
||||||
Reference in New Issue
Block a user