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

This commit is contained in:
Justin Hileman
2010-04-06 01:00:49 -04:00
32 changed files with 211 additions and 2 deletions
+5 -2
View File
@@ -22,6 +22,9 @@ class Mustache {
protected $throwPartialExceptions = false; protected $throwPartialExceptions = false;
protected $throwVariableExceptions = false; protected $throwVariableExceptions = false;
// Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8.
protected $charset = 'UTF-8';
protected $tagRegEx; protected $tagRegEx;
protected $template = ''; protected $template = '';
@@ -232,7 +235,7 @@ class Mustache {
* @return string * @return string
*/ */
protected function renderEscaped($tag_name, &$context) { 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); $otag = $this->prepareRegEx($this->otag);
$ctag = $this->prepareRegEx($this->ctag); $ctag = $this->prepareRegEx($this->ctag);
$this->tagRegEx = '/' . $otag . "(=|!|>|\\{|%)?([^\/#]+?)\\1?" . $ctag . "+/"; $this->tagRegEx = '/' . $otag . "(#|\/|=|!|>|\\{|&)?([^\/#]+?)\\1?" . $ctag . "+/";
return ''; return '';
} }
+35
View File
@@ -26,6 +26,31 @@ And a more in-depth example--this is the canonical Mustache template:
{{/in_ca}} {{/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: Create a view object--which could also be an associative array, but those don't do functions quite as well:
<?php <?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 See Also
-------- --------
+7
View File
@@ -0,0 +1,7 @@
<?php
class Comments extends Mustache {
public function title() {
return 'A Comedy of Errors';
}
}
+1
View File
@@ -0,0 +1 @@
<h1>{{title}}{{! just something interesting... or not... }}</h1>
+1
View File
@@ -0,0 +1 @@
<h1>A Comedy of Errors</h1>
+16
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -0,0 +1,8 @@
{{=<% %>=}}
* <% start %>
<%=| |=%>
|# middle |
* | item |
|/ middle |
|={{ }}=|
* {{ final }}
+4
View File
@@ -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.
@@ -0,0 +1,9 @@
<?php
class DoubleSection extends Mustache {
public function t() {
return true;
}
public $two = "second";
}
@@ -0,0 +1,7 @@
{{#t}}
* first
{{/t}}
* {{two}}
{{#t}}
* third
{{/t}}
@@ -0,0 +1,3 @@
* first
* second
* third
+5
View File
@@ -0,0 +1,5 @@
<?php
class Escaped extends Mustache {
public $title = "Bear > Shark";
}
+1
View File
@@ -0,0 +1 @@
<h1>{{title}}</h1>
+1
View File
@@ -0,0 +1 @@
<h1>Bear &gt; Shark</h1>
+14
View File
@@ -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
View File
@@ -0,0 +1,5 @@
* {{ start }}
{{# middle }}
* {{ item }}
{{/ middle }}
* {{ final }}
+4
View File
@@ -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
View File
@@ -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
View File
@@ -0,0 +1,5 @@
Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{ taxed_value }}, after taxes.
{{/in_ca}}
+3
View File
@@ -0,0 +1,3 @@
Hello Chris
You have just won $10000!
Well, $6000, after taxes.
+5
View File
@@ -0,0 +1,5 @@
<?php
class Unescaped extends Mustache {
public $title = "Bear > Shark";
}
+1
View File
@@ -0,0 +1 @@
<h1>{{{title}}}</h1>
+1
View File
@@ -0,0 +1 @@
<h1>Bear > Shark</h1>
+5
View File
@@ -0,0 +1,5 @@
<?php
class UTF8Unescaped extends Mustache {
public $test = '中文又来啦';
}
+1
View File
@@ -0,0 +1 @@
<h1>中文 {{test}}</h1>
+1
View File
@@ -0,0 +1 @@
<h1>中文 中文又来啦</h1>
@@ -0,0 +1,5 @@
<?php
class UTF8 extends Mustache {
public $test = '中文又来啦';
}
@@ -0,0 +1 @@
<h1>中文 {{{test}}}</h1>
@@ -0,0 +1 @@
<h1>中文 中文又来啦</h1>