From 9e803ed03336f0873f4d75ca320c5a52d8c51814 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 18 May 2010 10:28:01 -0400 Subject: [PATCH] Fix for stripping leading/trailing whitespace from partial names, per http://github.com/janl/mustache.js/issues/issue/34/#comment_244396 --- Mustache.php | 2 +- examples/whitespace/Whitespace.php | 37 +++++++++++++++++++++++++ examples/whitespace/whitespace.mustache | 10 +++++++ examples/whitespace/whitespace.txt | 12 ++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 examples/whitespace/Whitespace.php create mode 100644 examples/whitespace/whitespace.mustache create mode 100644 examples/whitespace/whitespace.txt diff --git a/Mustache.php b/Mustache.php index 85dae70..36c0a87 100644 --- a/Mustache.php +++ b/Mustache.php @@ -144,7 +144,7 @@ class Mustache { protected function _renderSection($template, &$context) { $otag = $this->_prepareRegEx($this->_otag); $ctag = $this->_prepareRegEx($this->_ctag); - $regex = '/' . $otag . '(\\^|\\#)(.+?)' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\2' . $ctag . '\\s*/m'; + $regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m'; $matches = array(); while (preg_match($regex, $template, $matches, PREG_OFFSET_CAPTURE)) { diff --git a/examples/whitespace/Whitespace.php b/examples/whitespace/Whitespace.php new file mode 100644 index 0000000..3be9689 --- /dev/null +++ b/examples/whitespace/Whitespace.php @@ -0,0 +1,37 @@ + tag }}` and `{{> tag}}` and `{{>tag}}` should all be equivalent. + * + * @extends Mustache + */ +class Whitespace extends Mustache { + public $foo = 'alpha'; + + public $bar = 'beta'; + + public function baz() { + return 'gamma'; + } + + public function qux() { + return array( + array('key with space' => 'A'), + array('key with space' => 'B'), + array('key with space' => 'C'), + array('key with space' => 'D'), + array('key with space' => 'E'), + array('key with space' => 'F'), + array('key with space' => 'G'), + ); + } + + protected $_partials = array( + 'alphabet' => " * {{.}}\n", + ); +} \ No newline at end of file diff --git a/examples/whitespace/whitespace.mustache b/examples/whitespace/whitespace.mustache new file mode 100644 index 0000000..0b3ba00 --- /dev/null +++ b/examples/whitespace/whitespace.mustache @@ -0,0 +1,10 @@ +{{^ inverted section test }} +These are some things: +{{/inverted section test }} +* {{ foo }} +* {{ bar}} +* {{ baz }} +{{# qux }} +* {{ key with space }} +{{/ qux }} +{{#qux}}.{{/qux}} \ No newline at end of file diff --git a/examples/whitespace/whitespace.txt b/examples/whitespace/whitespace.txt new file mode 100644 index 0000000..5226c69 --- /dev/null +++ b/examples/whitespace/whitespace.txt @@ -0,0 +1,12 @@ +These are some things: +* alpha +* beta +* gamma +* A +* B +* C +* D +* E +* F +* G +....... \ No newline at end of file