Skip to content

Commit

Permalink
Merge branch 'hotfix/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Apr 4, 2012
2 parents 6c752e1 + 417b139 commit cd6bfae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "test/spec"]
path = test/spec
url = http://github.com/mustache/spec.git
url = git://github.com/mustache/spec.git
25 changes: 23 additions & 2 deletions Mustache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class Mustache {

const VERSION = '1.0.0';
const VERSION = '1.1.0';
const SPEC_VERSION = '1.1.2';

/**
Expand All @@ -30,6 +30,9 @@ class Mustache {
MustacheException::UNKNOWN_PRAGMA => true,
);

// Override the escaper function. Defaults to `htmlspecialchars`.
protected $_escape;

// Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8.
protected $_charset = 'UTF-8';

Expand Down Expand Up @@ -84,6 +87,11 @@ class Mustache {
* Passing an $options array allows overriding certain Mustache options during instantiation:
*
* $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' => 'ISO-8859-1',
*
Expand Down Expand Up @@ -127,6 +135,13 @@ public function __construct($template = null, $view = null, $partials = null, ar
* @return void
*/
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'])) {
$this->_charset = $options['charset'];
}
Expand Down Expand Up @@ -640,7 +655,13 @@ protected function _stringHasR($str) {
* @return string
*/
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;
}

Expand Down
8 changes: 4 additions & 4 deletions examples/i18n/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class I18n extends Mustache {
public $__ = array(__CLASS__, '__trans');

// A *very* small i18n dictionary :)
private $dictionary = array(
private static $dictionary = array(
'Hello.' => 'Hola.',
'My name is {{ name }}.' => 'Me llamo {{ name }}.',
);

public function __trans($text) {
return isset($this->dictionary[$text]) ? $this->dictionary[$text] : $text;
public static function __trans($text) {
return isset(self::$dictionary[$text]) ? self::$dictionary[$text] : $text;
}
}
}

0 comments on commit cd6bfae

Please sign in to comment.