Merge branch 'hotfix/1.1.0'

This commit is contained in:
Justin Hileman
2012-04-03 22:01:06 -07:00
3 changed files with 28 additions and 7 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
[submodule "test/spec"] [submodule "test/spec"]
path = test/spec path = test/spec
url = http://github.com/mustache/spec.git url = git://github.com/mustache/spec.git
+23 -2
View File
@@ -14,7 +14,7 @@
*/ */
class Mustache { class Mustache {
const VERSION = '1.0.0'; const VERSION = '1.1.0';
const SPEC_VERSION = '1.1.2'; const SPEC_VERSION = '1.1.2';
/** /**
@@ -30,6 +30,9 @@ class Mustache {
MustacheException::UNKNOWN_PRAGMA => true, MustacheException::UNKNOWN_PRAGMA => true,
); );
// Override the escaper function. Defaults to `htmlspecialchars`.
protected $_escape;
// Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8. // Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8.
protected $_charset = 'UTF-8'; protected $_charset = 'UTF-8';
@@ -84,6 +87,11 @@ class Mustache {
* Passing an $options array allows overriding certain Mustache options during instantiation: * Passing an $options array allows overriding certain Mustache options during instantiation:
* *
* $options = array( * $options = array(
* // `escape` -- custom escaper callback; must be callable.
* 'escape' => function($text) {
* return htmlspecialchars($text, ENT_COMPAT, 'UTF-8');
* },
*
* // `charset` -- must be supported by `htmlspecialentities()`. defaults to 'UTF-8' * // `charset` -- must be supported by `htmlspecialentities()`. defaults to 'UTF-8'
* 'charset' => 'ISO-8859-1', * 'charset' => 'ISO-8859-1',
* *
@@ -127,6 +135,13 @@ class Mustache {
* @return void * @return void
*/ */
protected function _setOptions(array $options) { protected function _setOptions(array $options) {
if (isset($options['escape'])) {
if (!is_callable($options['escape'])) {
throw new InvalidArgumentException('Mustache constructor "escape" option must be callable');
}
$this->_escape = $options['escape'];
}
if (isset($options['charset'])) { if (isset($options['charset'])) {
$this->_charset = $options['charset']; $this->_charset = $options['charset'];
} }
@@ -640,7 +655,13 @@ class Mustache {
* @return string * @return string
*/ */
protected function _renderEscaped($tag_name, $leading, $trailing) { protected function _renderEscaped($tag_name, $leading, $trailing) {
$rendered = htmlentities($this->_renderUnescaped($tag_name, '', ''), ENT_COMPAT, $this->_charset); $value = $this->_renderUnescaped($tag_name, '', '');
if (isset($this->_escape)) {
$rendered = call_user_func($this->_escape, $value);
} else {
$rendered = htmlentities($value, ENT_COMPAT, $this->_charset);
}
return $leading . $rendered . $trailing; return $leading . $rendered . $trailing;
} }
+3 -3
View File
@@ -9,12 +9,12 @@ class I18n extends Mustache {
public $__ = array(__CLASS__, '__trans'); public $__ = array(__CLASS__, '__trans');
// A *very* small i18n dictionary :) // A *very* small i18n dictionary :)
private $dictionary = array( private static $dictionary = array(
'Hello.' => 'Hola.', 'Hello.' => 'Hola.',
'My name is {{ name }}.' => 'Me llamo {{ name }}.', 'My name is {{ name }}.' => 'Me llamo {{ name }}.',
); );
public function __trans($text) { public static function __trans($text) {
return isset($this->dictionary[$text]) ? $this->dictionary[$text] : $text; return isset(self::$dictionary[$text]) ? self::$dictionary[$text] : $text;
} }
} }