Merge branch 'master' of github.com:bobthecow/mustache.php into dev
This commit is contained in:
+5
-2
@@ -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 '';
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
--------
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
class Comments extends Mustache {
|
||||
public function title() {
|
||||
return 'A Comedy of Errors';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<h1>{{title}}{{! just something interesting... or not... }}</h1>
|
||||
@@ -0,0 +1 @@
|
||||
<h1>A Comedy of Errors</h1>
|
||||
@@ -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}}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
@@ -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.";
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{{=<% %>=}}
|
||||
* <% start %>
|
||||
<%=| |=%>
|
||||
|# middle |
|
||||
* | item |
|
||||
|/ middle |
|
||||
|={{ }}=|
|
||||
* {{ final }}
|
||||
@@ -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
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class Escaped extends Mustache {
|
||||
public $title = "Bear > Shark";
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<h1>{{title}}</h1>
|
||||
@@ -0,0 +1 @@
|
||||
<h1>Bear > Shark</h1>
|
||||
@@ -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.";
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
* {{ start }}
|
||||
{{# middle }}
|
||||
* {{ item }}
|
||||
{{/ middle }}
|
||||
* {{ final }}
|
||||
@@ -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,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;
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
Hello {{name}}
|
||||
You have just won ${{value}}!
|
||||
{{#in_ca}}
|
||||
Well, ${{ taxed_value }}, after taxes.
|
||||
{{/in_ca}}
|
||||
@@ -0,0 +1,3 @@
|
||||
Hello Chris
|
||||
You have just won $10000!
|
||||
Well, $6000, after taxes.
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class Unescaped extends Mustache {
|
||||
public $title = "Bear > Shark";
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<h1>{{{title}}}</h1>
|
||||
@@ -0,0 +1 @@
|
||||
<h1>Bear > Shark</h1>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class UTF8Unescaped extends Mustache {
|
||||
public $test = '中文又来啦';
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<h1>中文 {{test}}</h1>
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user