From fc170fca1103d68a4b1ae31cbfb4a9c4daf64105 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 26 Apr 2010 18:58:58 -0400 Subject: [PATCH 1/2] Add inverted section. --- Mustache.php | 35 +++++++++++++------ examples/inverted_section/InvertedSection.php | 5 +++ .../inverted_section.mustache | 2 ++ .../inverted_section/inverted_section.txt | 1 + 4 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 examples/inverted_section/InvertedSection.php create mode 100644 examples/inverted_section/inverted_section.mustache create mode 100644 examples/inverted_section/inverted_section.txt diff --git a/Mustache.php b/Mustache.php index d8d42f6..1b53c39 100644 --- a/Mustache.php +++ b/Mustache.php @@ -106,7 +106,7 @@ class Mustache { } /** - * Render boolean and enumerable sections. + * Render boolean, enumerable and inverted sections. * * @access protected * @param string $template @@ -120,23 +120,36 @@ 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); - if (is_array($val)) { - foreach ($val as $local_context) { - $replace .= $this->_render($content, $this->getContext($context, $local_context)); - } - } else if ($val) { - $replace .= $content; + 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)); + } + } 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 ''; } diff --git a/examples/inverted_section/InvertedSection.php b/examples/inverted_section/InvertedSection.php new file mode 100644 index 0000000..18eeb86 --- /dev/null +++ b/examples/inverted_section/InvertedSection.php @@ -0,0 +1,5 @@ +{{name}}{{/repo}} +{{^repo}}No repos :({{/repo}} \ No newline at end of file diff --git a/examples/inverted_section/inverted_section.txt b/examples/inverted_section/inverted_section.txt new file mode 100644 index 0000000..2b9ed3f --- /dev/null +++ b/examples/inverted_section/inverted_section.txt @@ -0,0 +1 @@ +No repos :( \ No newline at end of file From 57a6aafff1ebe15490a1c257fe84c0e6d9499cc0 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 26 Apr 2010 19:01:57 -0400 Subject: [PATCH 2/2] Fixed complex example to use inverted sections rather than fancy ruby-ish context swizzling (which never worked in PHP anyway). --- README.markdown | 1 - examples/complex/complex.mustache | 4 ++-- examples/complex/complex.php | 5 ----- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/README.markdown b/README.markdown index bc50bf5..0634a0f 100644 --- a/README.markdown +++ b/README.markdown @@ -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. diff --git a/examples/complex/complex.mustache b/examples/complex/complex.mustache index f3d8fea..4bf1687 100644 --- a/examples/complex/complex.mustache +++ b/examples/complex/complex.mustache @@ -5,9 +5,9 @@ {{#current}}
  • {{name}}
  • {{/current}} - {{#isLink}} + {{^current}}
  • {{name}}
  • - {{/isLink}} + {{/current}} {{/item}} {{/notEmpty}} diff --git a/examples/complex/complex.php b/examples/complex/complex.php index ca32ed7..ea55418 100644 --- a/examples/complex/complex.php +++ b/examples/complex/complex.php @@ -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()); }