diff --git a/Mustache.php b/Mustache.php index 838d21b..d8d42f6 100644 --- a/Mustache.php +++ b/Mustache.php @@ -22,6 +22,9 @@ class Mustache { protected $throwPartialExceptions = false; protected $throwVariableExceptions = false; + // Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8. + protected $charset = 'UTF-8'; + protected $tagRegEx; protected $template = ''; @@ -232,7 +235,7 @@ class Mustache { * @return string */ protected function renderEscaped($tag_name, &$context) { - return htmlentities($this->getVariable($tag_name, $context)); + return htmlentities($this->getVariable($tag_name, $context), null, $this->charset); } /** @@ -290,7 +293,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/README.markdown b/README.markdown index 7097147..bc50bf5 100644 --- a/README.markdown +++ b/README.markdown @@ -26,6 +26,31 @@ And a more in-depth example--this is the canonical Mustache template: {{/in_ca}} +Along with the associated Mustache class: + + value - ($this->value * 0.4); + } + + public $in_ca = true; + } + + +Render it like so: + + render($template); + ?> + + +Here's the same thing, a different way: + Create a view object--which could also be an associative array, but those don't do functions quite as well: + + +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. + + See Also -------- diff --git a/examples/comments/Comments.php b/examples/comments/Comments.php new file mode 100644 index 0000000..7f028ba --- /dev/null +++ b/examples/comments/Comments.php @@ -0,0 +1,7 @@ +{{title}}{{! just something interesting... or not... }} \ No newline at end of file diff --git a/examples/comments/comments.txt b/examples/comments/comments.txt new file mode 100644 index 0000000..9f40e77 --- /dev/null +++ b/examples/comments/comments.txt @@ -0,0 +1 @@ +

A Comedy of Errors

\ No newline at end of file diff --git a/examples/complex/complex.mustache b/examples/complex/complex.mustache new file mode 100644 index 0000000..f3d8fea --- /dev/null +++ b/examples/complex/complex.mustache @@ -0,0 +1,16 @@ +

{{header}}

+{{#notEmpty}} + +{{/notEmpty}} +{{#isEmpty}} +

The list is empty.

+{{/isEmpty}} \ No newline at end of file diff --git a/examples/complex/complex.php b/examples/complex/complex.php new file mode 100644 index 0000000..ca32ed7 --- /dev/null +++ b/examples/complex/complex.php @@ -0,0 +1,24 @@ + 'red', 'current' => true, 'url' => '#Red'), + array('name' => 'green', 'current' => false, 'url' => '#Green'), + 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()); + } + + public function isEmpty() { + return count($this->item) === 0; + } +} \ No newline at end of file diff --git a/examples/complex/complex.txt b/examples/complex/complex.txt new file mode 100644 index 0000000..eb78d45 --- /dev/null +++ b/examples/complex/complex.txt @@ -0,0 +1,6 @@ +

Colors

+ diff --git a/examples/delimiters/Delimiters.php b/examples/delimiters/Delimiters.php new file mode 100644 index 0000000..74ab583 --- /dev/null +++ b/examples/delimiters/Delimiters.php @@ -0,0 +1,14 @@ + "And it worked the second time."), + array('item' => "As well as the third."), + ); + } + + public $final = "Then, surprisingly, it worked the final time."; +} \ No newline at end of file diff --git a/examples/delimiters/delimiters.mustache b/examples/delimiters/delimiters.mustache new file mode 100644 index 0000000..e9b0332 --- /dev/null +++ b/examples/delimiters/delimiters.mustache @@ -0,0 +1,8 @@ +{{=<% %>=}} +* <% start %> +<%=| |=%> +|# middle | +* | item | +|/ middle | +|={{ }}=| +* {{ final }} \ No newline at end of file diff --git a/examples/delimiters/delimiters.txt b/examples/delimiters/delimiters.txt new file mode 100644 index 0000000..e6b2d7a --- /dev/null +++ b/examples/delimiters/delimiters.txt @@ -0,0 +1,4 @@ +* It worked the first time. +* And it worked the second time. +* As well as the third. +* Then, surprisingly, it worked the final time. \ No newline at end of file diff --git a/examples/double_section/DoubleSection.php b/examples/double_section/DoubleSection.php new file mode 100644 index 0000000..8aa1ac6 --- /dev/null +++ b/examples/double_section/DoubleSection.php @@ -0,0 +1,9 @@ + Shark"; +} \ No newline at end of file diff --git a/examples/escaped/escaped.mustache b/examples/escaped/escaped.mustache new file mode 100644 index 0000000..8be4ccb --- /dev/null +++ b/examples/escaped/escaped.mustache @@ -0,0 +1 @@ +

{{title}}

\ No newline at end of file diff --git a/examples/escaped/escaped.txt b/examples/escaped/escaped.txt new file mode 100644 index 0000000..dcab18d --- /dev/null +++ b/examples/escaped/escaped.txt @@ -0,0 +1 @@ +

Bear > Shark

\ No newline at end of file diff --git a/examples/sections/Sections.php b/examples/sections/Sections.php new file mode 100644 index 0000000..baa61d7 --- /dev/null +++ b/examples/sections/Sections.php @@ -0,0 +1,14 @@ + "And it worked the second time."), + array('item' => "As well as the third."), + ); + } + + public $final = "Then, surprisingly, it worked the final time."; +} \ No newline at end of file diff --git a/examples/sections/sections.mustache b/examples/sections/sections.mustache new file mode 100644 index 0000000..44dfce4 --- /dev/null +++ b/examples/sections/sections.mustache @@ -0,0 +1,5 @@ +* {{ start }} +{{# middle }} +* {{ item }} +{{/ middle }} +* {{ final }} \ No newline at end of file diff --git a/examples/sections/sections.txt b/examples/sections/sections.txt new file mode 100644 index 0000000..e6b2d7a --- /dev/null +++ b/examples/sections/sections.txt @@ -0,0 +1,4 @@ +* It worked the first time. +* And it worked the second time. +* As well as the third. +* Then, surprisingly, it worked the final time. \ No newline at end of file diff --git a/examples/simple/Simple.php b/examples/simple/Simple.php new file mode 100644 index 0000000..6d07dac --- /dev/null +++ b/examples/simple/Simple.php @@ -0,0 +1,12 @@ +value - ($this->value * 0.4); + } + + public $in_ca = true; +}; \ No newline at end of file diff --git a/examples/simple/simple.mustache b/examples/simple/simple.mustache new file mode 100644 index 0000000..03df206 --- /dev/null +++ b/examples/simple/simple.mustache @@ -0,0 +1,5 @@ +Hello {{name}} +You have just won ${{value}}! +{{#in_ca}} +Well, ${{ taxed_value }}, after taxes. +{{/in_ca}} \ No newline at end of file diff --git a/examples/simple/simple.txt b/examples/simple/simple.txt new file mode 100644 index 0000000..5d75d65 --- /dev/null +++ b/examples/simple/simple.txt @@ -0,0 +1,3 @@ +Hello Chris +You have just won $10000! +Well, $6000, after taxes. diff --git a/examples/unescaped/Unescaped.php b/examples/unescaped/Unescaped.php new file mode 100644 index 0000000..41b10cb --- /dev/null +++ b/examples/unescaped/Unescaped.php @@ -0,0 +1,5 @@ + Shark"; +} \ No newline at end of file diff --git a/examples/unescaped/unescaped.mustache b/examples/unescaped/unescaped.mustache new file mode 100644 index 0000000..9982708 --- /dev/null +++ b/examples/unescaped/unescaped.mustache @@ -0,0 +1 @@ +

{{{title}}}

\ No newline at end of file diff --git a/examples/unescaped/unescaped.txt b/examples/unescaped/unescaped.txt new file mode 100644 index 0000000..01fa404 --- /dev/null +++ b/examples/unescaped/unescaped.txt @@ -0,0 +1 @@ +

Bear > Shark

\ No newline at end of file diff --git a/examples/utf8/UTF8.php b/examples/utf8/UTF8.php new file mode 100644 index 0000000..7843f53 --- /dev/null +++ b/examples/utf8/UTF8.php @@ -0,0 +1,5 @@ +中文 {{test}} \ No newline at end of file diff --git a/examples/utf8/utf8.txt b/examples/utf8/utf8.txt new file mode 100644 index 0000000..bf17971 --- /dev/null +++ b/examples/utf8/utf8.txt @@ -0,0 +1 @@ +

中文 中文又来啦

\ No newline at end of file diff --git a/examples/utf8_unescaped/UTF8Unescaped.php b/examples/utf8_unescaped/UTF8Unescaped.php new file mode 100644 index 0000000..d097cbe --- /dev/null +++ b/examples/utf8_unescaped/UTF8Unescaped.php @@ -0,0 +1,5 @@ +中文 {{{test}}} \ No newline at end of file diff --git a/examples/utf8_unescaped/utf8_unescaped.txt b/examples/utf8_unescaped/utf8_unescaped.txt new file mode 100644 index 0000000..bf17971 --- /dev/null +++ b/examples/utf8_unescaped/utf8_unescaped.txt @@ -0,0 +1 @@ +

中文 中文又来啦

\ No newline at end of file