Jelajahi Sumber

Merge branch 'master' of github.com:bobthecow/mustache.php into dev

Justin Hileman 15 tahun lalu
induk
melakukan
06ea054219

+ 5 - 2
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 '';
 	}
 

+ 35 - 0
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:
+
+    <?php
+    class Chris extends Mustache {
+        public $name = "Chris";
+        public $value = 10000;
+    
+        public function taxed_value() {
+            return $this->value - ($this->value * 0.4);
+        }
+    
+        public $in_ca = true;
+    }
+
+
+Render it like so:
+
+    <?php
+    $c = new Chris;
+    echo $chris->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:
 
     <?php
@@ -51,6 +76,16 @@ And render it:
     ?>
 
 
+
+
+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
 --------
 

+ 7 - 0
examples/comments/Comments.php

@@ -0,0 +1,7 @@
+<?php
+
+class Comments extends Mustache {
+	public function title() {
+		return 'A Comedy of Errors';
+	}
+}

+ 1 - 0
examples/comments/comments.mustache

@@ -0,0 +1 @@
+<h1>{{title}}{{! just something interesting... or not... }}</h1>

+ 1 - 0
examples/comments/comments.txt

@@ -0,0 +1 @@
+<h1>A Comedy of Errors</h1>

+ 16 - 0
examples/complex/complex.mustache

@@ -0,0 +1,16 @@
+<h1>{{header}}</h1>
+{{#notEmpty}}
+  <ul>
+  {{#item}}
+    {{#current}}
+      <li><strong>{{name}}</strong></li>
+    {{/current}}
+    {{#isLink}}
+      <li><a href="{{url}}">{{name}}</a></li>
+    {{/isLink}}
+  {{/item}}
+  </ul>
+{{/notEmpty}}
+{{#isEmpty}}
+  <p>The list is empty.</p>
+{{/isEmpty}}

+ 24 - 0
examples/complex/complex.php

@@ -0,0 +1,24 @@
+<?php
+
+class Complex extends Mustache {
+	public $header = 'Colors';
+	
+	public $item = array(
+		array('name' => '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;
+	}
+}

+ 6 - 0
examples/complex/complex.txt

@@ -0,0 +1,6 @@
+<h1>Colors</h1>
+<ul>
+  <li><strong>red</strong></li>
+    <li><a href="#Green">green</a></li>
+    <li><a href="#Blue">blue</a></li>
+    </ul>

+ 14 - 0
examples/delimiters/Delimiters.php

@@ -0,0 +1,14 @@
+<?php
+
+class Delimiters extends Mustache {
+	public $start = "It worked the first time.";
+	
+	public function middle() {
+		return array(
+			array('item' => "And it worked the second time."),
+			array('item' => "As well as the third."),
+		);
+	}
+	
+	public $final = "Then, surprisingly, it worked the final time.";
+}

+ 8 - 0
examples/delimiters/delimiters.mustache

@@ -0,0 +1,8 @@
+{{=<% %>=}}
+* <% start %>
+<%=| |=%>
+|# middle |
+* | item |
+|/ middle |
+|={{ }}=|
+* {{ final }}

+ 4 - 0
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.

+ 9 - 0
examples/double_section/DoubleSection.php

@@ -0,0 +1,9 @@
+<?php
+
+class DoubleSection extends Mustache {
+	public function t() {
+		return true;
+	}
+	
+	public $two = "second";
+}

+ 7 - 0
examples/double_section/double_section.mustache

@@ -0,0 +1,7 @@
+{{#t}}
+  * first
+{{/t}}
+* {{two}}
+{{#t}}
+  * third
+{{/t}}

+ 3 - 0
examples/double_section/double_section.txt

@@ -0,0 +1,3 @@
+* first
+* second
+* third

+ 5 - 0
examples/escaped/Escaped.php

@@ -0,0 +1,5 @@
+<?php
+
+class Escaped extends Mustache {
+	public $title = "Bear > Shark";
+}

+ 1 - 0
examples/escaped/escaped.mustache

@@ -0,0 +1 @@
+<h1>{{title}}</h1>

+ 1 - 0
examples/escaped/escaped.txt

@@ -0,0 +1 @@
+<h1>Bear &gt; Shark</h1>

+ 14 - 0
examples/sections/Sections.php

@@ -0,0 +1,14 @@
+<?php
+
+class Sections extends Mustache {
+	public $start = "It worked the first time.";
+	
+	public function middle() {
+		return array(
+			array('item' => "And it worked the second time."),
+			array('item' => "As well as the third."),
+		);
+	}
+	
+	public $final = "Then, surprisingly, it worked the final time.";
+}

+ 5 - 0
examples/sections/sections.mustache

@@ -0,0 +1,5 @@
+* {{ start }}
+{{# middle }}
+* {{ item }}
+{{/ middle }}
+* {{ final }}

+ 4 - 0
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.

+ 12 - 0
examples/simple/Simple.php

@@ -0,0 +1,12 @@
+<?php
+
+class Simple extends Mustache {
+	public $name = "Chris";
+	public $value = 10000;
+
+	public function taxed_value() {
+		return $this->value - ($this->value * 0.4);
+	}
+
+	public $in_ca = true;
+};

+ 5 - 0
examples/simple/simple.mustache

@@ -0,0 +1,5 @@
+Hello {{name}}
+You have just won ${{value}}!
+{{#in_ca}}
+Well, ${{ taxed_value }}, after taxes.
+{{/in_ca}}

+ 3 - 0
examples/simple/simple.txt

@@ -0,0 +1,3 @@
+Hello Chris
+You have just won $10000!
+Well, $6000, after taxes.

+ 5 - 0
examples/unescaped/Unescaped.php

@@ -0,0 +1,5 @@
+<?php
+
+class Unescaped extends Mustache {
+	public $title = "Bear > Shark";
+}

+ 1 - 0
examples/unescaped/unescaped.mustache

@@ -0,0 +1 @@
+<h1>{{{title}}}</h1>

+ 1 - 0
examples/unescaped/unescaped.txt

@@ -0,0 +1 @@
+<h1>Bear > Shark</h1>

+ 5 - 0
examples/utf8/UTF8.php

@@ -0,0 +1,5 @@
+<?php
+
+class UTF8Unescaped extends Mustache {
+	public $test = '中文又来啦';
+}

+ 1 - 0
examples/utf8/utf8.mustache

@@ -0,0 +1 @@
+<h1>中文 {{test}}</h1>

+ 1 - 0
examples/utf8/utf8.txt

@@ -0,0 +1 @@
+<h1>中文 中文又来啦</h1>

+ 5 - 0
examples/utf8_unescaped/UTF8Unescaped.php

@@ -0,0 +1,5 @@
+<?php
+
+class UTF8 extends Mustache {
+	public $test = '中文又来啦';
+}

+ 1 - 0
examples/utf8_unescaped/utf8_unescaped.mustache

@@ -0,0 +1 @@
+<h1>中文 {{{test}}}</h1>

+ 1 - 0
examples/utf8_unescaped/utf8_unescaped.txt

@@ -0,0 +1 @@
+<h1>中文 中文又来啦</h1>