From b1c3e06144c96b57c3e3107cd2d4fe9cf09b6083 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 29 Mar 2010 14:25:31 -0400 Subject: [PATCH 01/11] Added a second example. It's still not enough, but better than nothing. --- README.markdown | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.markdown b/README.markdown index 7097147..108a0a8 100644 --- a/README.markdown +++ b/README.markdown @@ -50,6 +50,24 @@ And render it: echo $m->render($template, $chris); ?> +Here's the same thing, a different way: + + value - ($this->value * 0.4); + } + + public $in_ca = true; + } + + $c = new Chris; + echo $chris->render($template); + ?> + See Also -------- From a51146f5e2cffe6bac6939b014f57cb757f8b9ac Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 29 Mar 2010 14:45:15 -0400 Subject: [PATCH 02/11] Fixed RegEx used in changeDelimiter call to match initial RegEx string. This doesn't really need the section tag modifiers (# and /) but they are included here for consistency. --- Mustache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index 838d21b..578d060 100644 --- a/Mustache.php +++ b/Mustache.php @@ -290,7 +290,7 @@ class Mustache { $otag = $this->prepareRegEx($this->otag); $ctag = $this->prepareRegEx($this->ctag); - $this->tagRegEx = '/' . $otag . "(=|!|>|\\{|%)?([^\/#]+?)\\1?" . $ctag . "+/"; + $this->tagRegEx = '/' . $otag . "(#|\/|=|!|>|\\{|&)?([^\/#]+?)\\1?" . $ctag . "+/"; return ''; } From 5e99b152521cd2b072e82298d56b8839ab92b7de Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 30 Mar 2010 14:57:18 -0400 Subject: [PATCH 03/11] Adding examples. --- examples/comments/Comments.php | 7 +++++++ examples/comments/comments.mustache | 1 + examples/comments/comments.txt | 1 + examples/complex/complex.mustache | 16 ++++++++++++++++ examples/complex/complex.php | 24 ++++++++++++++++++++++++ examples/complex/complex.txt | 6 ++++++ examples/simple/Simple.php | 12 ++++++++++++ examples/simple/simple.mustache | 5 +++++ examples/simple/simple.txt | 3 +++ examples/unescaped/Unescaped.php | 5 +++++ examples/unescaped/unescaped.mustache | 1 + examples/unescaped/unescaped.txt | 1 + 12 files changed, 82 insertions(+) create mode 100644 examples/comments/Comments.php create mode 100644 examples/comments/comments.mustache create mode 100644 examples/comments/comments.txt create mode 100644 examples/complex/complex.mustache create mode 100644 examples/complex/complex.php create mode 100644 examples/complex/complex.txt create mode 100644 examples/simple/Simple.php create mode 100644 examples/simple/simple.mustache create mode 100644 examples/simple/simple.txt create mode 100644 examples/unescaped/Unescaped.php create mode 100644 examples/unescaped/unescaped.mustache create mode 100644 examples/unescaped/unescaped.txt 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..e7ca1b5 --- /dev/null +++ b/examples/complex/complex.mustache @@ -0,0 +1,16 @@ +

{{header}}

+{{#list}} +
    + {{#item}} + {{#current}} +
  • {{name}}
  • + {{/current}} + {{#link}} +
  • {{name}}
  • + {{/link}} + {{/item}} +
+{{/list}} +{{#empty}} +

The list is empty.

+{{/empty}} \ No newline at end of file diff --git a/examples/complex/complex.php b/examples/complex/complex.php new file mode 100644 index 0000000..adc8291 --- /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 link() { + // Exploit the fact that the current iteration item is at the top of the context stack. + return $this->getVariable($current) != true; + } + + public function list() { + return !($this->empty()); + } + + public function empty() { + 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..5504ffc --- /dev/null +++ b/examples/complex/complex.txt @@ -0,0 +1,6 @@ +

Colors

+ \ 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..44403a8 --- /dev/null +++ b/examples/simple/simple.txt @@ -0,0 +1,3 @@ +Hello Chris +You have just won $10000! +Well, $6000, after taxes. \ No newline at end of file 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 From a93feb2f788b690ac89f4b607887267c6126feb1 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 5 Apr 2010 23:54:10 -0400 Subject: [PATCH 04/11] Fixed trailing newline in simple example output. --- examples/simple/simple.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple/simple.txt b/examples/simple/simple.txt index 44403a8..5d75d65 100644 --- a/examples/simple/simple.txt +++ b/examples/simple/simple.txt @@ -1,3 +1,3 @@ Hello Chris You have just won $10000! -Well, $6000, after taxes. \ No newline at end of file +Well, $6000, after taxes. From 26aad7b4903b061deba911e44331dfafe99efcb0 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 5 Apr 2010 23:55:29 -0400 Subject: [PATCH 05/11] Complex example was a direct port of ruby/javascript complex example, resulting in some methods named with PHP reserved keywords. Fixing that oversight. --- examples/complex/complex.mustache | 12 ++++++------ examples/complex/complex.php | 10 +++++----- examples/complex/complex.txt | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/complex/complex.mustache b/examples/complex/complex.mustache index e7ca1b5..f3d8fea 100644 --- a/examples/complex/complex.mustache +++ b/examples/complex/complex.mustache @@ -1,16 +1,16 @@

{{header}}

-{{#list}} +{{#notEmpty}}
    {{#item}} {{#current}}
  • {{name}}
  • {{/current}} - {{#link}} + {{#isLink}}
  • {{name}}
  • - {{/link}} + {{/isLink}} {{/item}}
-{{/list}} -{{#empty}} +{{/notEmpty}} +{{#isEmpty}}

The list is empty.

-{{/empty}} \ No newline at end of file +{{/isEmpty}} \ No newline at end of file diff --git a/examples/complex/complex.php b/examples/complex/complex.php index adc8291..ca32ed7 100644 --- a/examples/complex/complex.php +++ b/examples/complex/complex.php @@ -9,16 +9,16 @@ class Complex extends Mustache { array('name' => 'blue', 'current' => false, 'url' => '#Blue'), ); - public function link() { + public function isLink() { // Exploit the fact that the current iteration item is at the top of the context stack. - return $this->getVariable($current) != true; + return $this->getVariable('current', $this->context) != true; } - public function list() { - return !($this->empty()); + public function notEmpty() { + return !($this->isEmpty()); } - public function empty() { + 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 index 5504ffc..eb78d45 100644 --- a/examples/complex/complex.txt +++ b/examples/complex/complex.txt @@ -3,4 +3,4 @@
  • red
  • green
  • blue
  • - \ No newline at end of file + From e77226c4f61688e354a7bd6bdebd1a10721a8d5e Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 6 Apr 2010 00:19:55 -0400 Subject: [PATCH 06/11] Added 'known issues' to readme. --- README.markdown | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.markdown b/README.markdown index 108a0a8..f128984 100644 --- a/README.markdown +++ b/README.markdown @@ -69,6 +69,13 @@ Here's the same thing, a different way: ?> +Known Issues +------------ + + * Sections don't respect delimiter changes -- `delimiters` example currently fails with an "unclosed section" exception. + * Test coverage is incomplete. + + See Also -------- From cb5985171cefd42ab900880cc0601899403deaa6 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 6 Apr 2010 00:23:49 -0400 Subject: [PATCH 07/11] Added known issue for complex complex example failure. --- README.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/README.markdown b/README.markdown index f128984..3e7b62b 100644 --- a/README.markdown +++ b/README.markdown @@ -73,6 +73,7 @@ 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. From 4059c4da0088d4ad149dae0c2ca536b17519391b Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 6 Apr 2010 00:24:33 -0400 Subject: [PATCH 08/11] Two new examples: 'delimiters' and 'sections'. Would be more exciting if 50% of new examples didn't fail :) --- examples/delimiters/Delimiters.php | 14 ++++++++++++++ examples/delimiters/delimiters.mustache | 8 ++++++++ examples/delimiters/delimiters.txt | 4 ++++ examples/sections/Sections.php | 14 ++++++++++++++ examples/sections/sections.mustache | 5 +++++ examples/sections/sections.txt | 4 ++++ 6 files changed, 49 insertions(+) create mode 100644 examples/delimiters/Delimiters.php create mode 100644 examples/delimiters/delimiters.mustache create mode 100644 examples/delimiters/delimiters.txt create mode 100644 examples/sections/Sections.php create mode 100644 examples/sections/sections.mustache create mode 100644 examples/sections/sections.txt 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/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 From fa8c81dcfd04d6b442a94122b59a105d7f5ce082 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 6 Apr 2010 00:28:02 -0400 Subject: [PATCH 09/11] Update README to reflect preferred mustache class implementation. Rendering arbitrary generic view class/array and template is secondary. --- README.markdown | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/README.markdown b/README.markdown index 3e7b62b..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: render($template, $chris); ?> -Here's the same thing, a different way: - value - ($this->value * 0.4); - } - - public $in_ca = true; - } - - $c = new Chris; - echo $chris->render($template); - ?> Known Issues From 4baf602eabccf7d9a3ee11fbbc1db01b9be1cb03 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 6 Apr 2010 00:53:32 -0400 Subject: [PATCH 10/11] Updated default character escaping for UTF-8 compatibility. For other character sets, or to disable UTF-8 support entirely, change the 'charset' property or renderEscaped method on subclasses. --- Mustache.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index 578d060..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); } /** From 1ede993bb328709d8a9ae97d00f181cfb9600ed4 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 6 Apr 2010 00:54:45 -0400 Subject: [PATCH 11/11] Added double section, escaped output, utf8 escaped and utf8 unescaped examples. --- examples/double_section/DoubleSection.php | 9 +++++++++ examples/double_section/double_section.mustache | 7 +++++++ examples/double_section/double_section.txt | 3 +++ examples/escaped/Escaped.php | 5 +++++ examples/escaped/escaped.mustache | 1 + examples/escaped/escaped.txt | 1 + examples/utf8/UTF8.php | 5 +++++ examples/utf8/utf8.mustache | 1 + examples/utf8/utf8.txt | 1 + examples/utf8_unescaped/UTF8Unescaped.php | 5 +++++ examples/utf8_unescaped/utf8_unescaped.mustache | 1 + examples/utf8_unescaped/utf8_unescaped.txt | 1 + 12 files changed, 40 insertions(+) create mode 100644 examples/double_section/DoubleSection.php create mode 100644 examples/double_section/double_section.mustache create mode 100644 examples/double_section/double_section.txt create mode 100644 examples/escaped/Escaped.php create mode 100644 examples/escaped/escaped.mustache create mode 100644 examples/escaped/escaped.txt create mode 100644 examples/utf8/UTF8.php create mode 100644 examples/utf8/utf8.mustache create mode 100644 examples/utf8/utf8.txt create mode 100644 examples/utf8_unescaped/UTF8Unescaped.php create mode 100644 examples/utf8_unescaped/utf8_unescaped.mustache create mode 100644 examples/utf8_unescaped/utf8_unescaped.txt 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/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