Merge branch 'release/1.0.0'
This commit is contained in:
+24
-3
@@ -14,7 +14,7 @@
|
||||
*/
|
||||
class Mustache {
|
||||
|
||||
const VERSION = '0.9.0';
|
||||
const VERSION = '1.0.0';
|
||||
const SPEC_VERSION = '1.1.2';
|
||||
|
||||
/**
|
||||
@@ -94,6 +94,15 @@ class Mustache {
|
||||
* 'pragmas' => array(
|
||||
* Mustache::PRAGMA_UNESCAPED => true
|
||||
* ),
|
||||
*
|
||||
* // an array of thrown exceptions to enable/disable
|
||||
* 'throws_exceptions' => array(
|
||||
* MustacheException::UNKNOWN_VARIABLE => false,
|
||||
* MustacheException::UNCLOSED_SECTION => true,
|
||||
* MustacheException::UNEXPECTED_CLOSE_SECTION => true,
|
||||
* MustacheException::UNKNOWN_PARTIAL => false,
|
||||
* MustacheException::UNKNOWN_PRAGMA => true,
|
||||
* ),
|
||||
* );
|
||||
*
|
||||
* @access public
|
||||
@@ -139,6 +148,12 @@ class Mustache {
|
||||
}
|
||||
$this->_pragmas = $options['pragmas'];
|
||||
}
|
||||
|
||||
if (isset($options['throws_exceptions'])) {
|
||||
foreach ($options['throws_exceptions'] as $exception => $value) {
|
||||
$this->_throwsExceptions[$exception] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,7 +204,7 @@ class Mustache {
|
||||
}
|
||||
|
||||
$template = $this->_renderPragmas($template);
|
||||
$template = $this->_renderTemplate($template, $this->_context);
|
||||
$template = $this->_renderTemplate($template);
|
||||
|
||||
$this->_otag = $otag_orig;
|
||||
$this->_ctag = $ctag_orig;
|
||||
@@ -403,7 +418,11 @@ class Mustache {
|
||||
$options_string = $matches['options_string'];
|
||||
|
||||
if (!in_array($pragma_name, $this->_pragmasImplemented)) {
|
||||
if ($this->_throwsException(MustacheException::UNKNOWN_PRAGMA)) {
|
||||
throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
$options = array();
|
||||
@@ -448,8 +467,10 @@ class Mustache {
|
||||
*/
|
||||
protected function _getPragmaOptions($pragma_name) {
|
||||
if (!$this->_hasPragma($pragma_name)) {
|
||||
if ($this->_throwsException(MustacheException::UNKNOWN_PRAGMA)) {
|
||||
throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
|
||||
}
|
||||
}
|
||||
|
||||
return (is_array($this->_localPragmas[$pragma_name])) ? $this->_localPragmas[$pragma_name] : array();
|
||||
}
|
||||
@@ -767,7 +788,7 @@ class Mustache {
|
||||
$first = array_shift($chunks);
|
||||
|
||||
$ret = $this->_findVariableInContext($first, $this->_context);
|
||||
while ($next = array_shift($chunks)) {
|
||||
foreach ($chunks as $next) {
|
||||
// Slice off a chunk of context for dot notation traversal.
|
||||
$c = array($ret);
|
||||
$ret = $this->_findVariableInContext($next, $c);
|
||||
|
||||
+12
-6
@@ -9,25 +9,29 @@ Usage
|
||||
|
||||
A quick example:
|
||||
|
||||
```php
|
||||
<?php
|
||||
include('Mustache.php');
|
||||
$m = new Mustache;
|
||||
echo $m->render('Hello {{planet}}', array('planet' => 'World!'));
|
||||
// "Hello World!"
|
||||
?>
|
||||
```
|
||||
|
||||
|
||||
And a more in-depth example--this is the canonical Mustache template:
|
||||
|
||||
```
|
||||
Hello {{name}}
|
||||
You have just won ${{value}}!
|
||||
{{#in_ca}}
|
||||
Well, ${{taxed_value}}, after taxes.
|
||||
{{/in_ca}}
|
||||
```
|
||||
|
||||
|
||||
Along with the associated Mustache class:
|
||||
|
||||
```php
|
||||
<?php
|
||||
class Chris extends Mustache {
|
||||
public $name = "Chris";
|
||||
@@ -39,20 +43,23 @@ Along with the associated Mustache class:
|
||||
|
||||
public $in_ca = true;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Render it like so:
|
||||
|
||||
```php
|
||||
<?php
|
||||
$chris = 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
|
||||
<?php
|
||||
class Chris {
|
||||
public $name = "Chris";
|
||||
@@ -64,18 +71,17 @@ Create a view object--which could also be an associative array, but those don't
|
||||
|
||||
public $in_ca = true;
|
||||
}
|
||||
?>
|
||||
```
|
||||
|
||||
|
||||
And render it:
|
||||
|
||||
```php
|
||||
<?php
|
||||
$chris = new Chris;
|
||||
$m = new Mustache;
|
||||
echo $m->render($template, $chris);
|
||||
?>
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
Known Issues
|
||||
|
||||
@@ -9,10 +9,11 @@ class DotNotation extends Mustache {
|
||||
public $person = array(
|
||||
'name' => array('first' => 'Chris', 'last' => 'Firescythe'),
|
||||
'age' => 24,
|
||||
'hobbies' => array('Cycling', 'Fishing'),
|
||||
'hometown' => array(
|
||||
'city' => 'Cincinnati',
|
||||
'state' => 'OH',
|
||||
)
|
||||
),
|
||||
);
|
||||
|
||||
public $normal = 'Normal';
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
* {{person.name.first}} {{person.name.last}}
|
||||
* {{person.age}}
|
||||
* {{person.hobbies.0}}, {{person.hobbies.1}}
|
||||
* {{person.hometown.city}}, {{person.hometown.state}}
|
||||
* {{normal}}
|
||||
@@ -1,4 +1,5 @@
|
||||
* Chris Firescythe
|
||||
* 24
|
||||
* Cycling, Fishing
|
||||
* Cincinnati, OH
|
||||
* Normal
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
require_once '../Mustache.php';
|
||||
|
||||
class MustacheCallTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testCallEatsContext() {
|
||||
$foo = new ClassWithCall();
|
||||
$foo->name = 'Bob';
|
||||
|
||||
$template = '{{# foo }}{{ label }}: {{ name }}{{/ foo }}';
|
||||
$data = array('label' => 'name', 'foo' => $foo);
|
||||
$m = new Mustache($template, $data);
|
||||
|
||||
$this->assertEquals('name: Bob', $m->render());
|
||||
}
|
||||
}
|
||||
|
||||
class ClassWithCall {
|
||||
public $name;
|
||||
public function __call($method, $args) {
|
||||
return 'unknown value';
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,34 @@ class MustacheExceptionTest extends PHPUnit_Framework_TestCase {
|
||||
$mustache = new TestableMustache();
|
||||
$mustache->testableGetPragmaOptions('PRAGMATIC');
|
||||
}
|
||||
|
||||
public function testOverrideThrownExceptionsViaConstructorOptions() {
|
||||
$exceptions = array(
|
||||
MustacheException::UNKNOWN_VARIABLE,
|
||||
MustacheException::UNCLOSED_SECTION,
|
||||
MustacheException::UNEXPECTED_CLOSE_SECTION,
|
||||
MustacheException::UNKNOWN_PARTIAL,
|
||||
MustacheException::UNKNOWN_PRAGMA,
|
||||
);
|
||||
|
||||
$one = new TestableMustache(null, null, null, array(
|
||||
'throws_exceptions' => array_fill_keys($exceptions, true)
|
||||
));
|
||||
|
||||
$thrownExceptions = $one->getThrownExceptions();
|
||||
foreach ($exceptions as $exception) {
|
||||
$this->assertTrue($thrownExceptions[$exception]);
|
||||
}
|
||||
|
||||
$two = new TestableMustache(null, null, null, array(
|
||||
'throws_exceptions' => array_fill_keys($exceptions, false)
|
||||
));
|
||||
|
||||
$thrownExceptions = $two->getThrownExceptions();
|
||||
foreach ($exceptions as $exception) {
|
||||
$this->assertFalse($thrownExceptions[$exception]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PickyMustache extends Mustache {
|
||||
@@ -117,4 +145,8 @@ class TestableMustache extends Mustache {
|
||||
public function testableGetPragmaOptions($pragma_name) {
|
||||
return $this->_getPragmaOptions($pragma_name);
|
||||
}
|
||||
|
||||
public function getThrownExceptions() {
|
||||
return $this->_throwsExceptions;
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,20 @@ class MustachePragmaTest extends PHPUnit_Framework_TestCase {
|
||||
$this->fail('Mustache should have thrown an unknown pragma exception');
|
||||
}
|
||||
|
||||
public function testSuppressUnknownPragmaException() {
|
||||
$m = new LessWhinyMustache();
|
||||
|
||||
try {
|
||||
$this->assertEquals('', $m->render('{{%I-HAVE-THE-GREATEST-MUSTACHE}}'));
|
||||
} catch (MustacheException $e) {
|
||||
if ($e->getCode() == MustacheException::UNKNOWN_PRAGMA) {
|
||||
$this->fail('Mustache should have thrown an unknown pragma exception');
|
||||
} else {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testPragmaReplace() {
|
||||
$m = new Mustache();
|
||||
$this->assertEquals('', $m->render('{{%UNESCAPED}}'), 'Pragma tag not removed');
|
||||
@@ -48,3 +62,13 @@ class MustachePragmaTest extends PHPUnit_Framework_TestCase {
|
||||
$this->assertEquals('>>>', $m->render('{{{symbol}}}'));
|
||||
}
|
||||
}
|
||||
|
||||
class LessWhinyMustache extends Mustache {
|
||||
protected $_throwsExceptions = array(
|
||||
MustacheException::UNKNOWN_VARIABLE => false,
|
||||
MustacheException::UNCLOSED_SECTION => true,
|
||||
MustacheException::UNEXPECTED_CLOSE_SECTION => true,
|
||||
MustacheException::UNKNOWN_PARTIAL => false,
|
||||
MustacheException::UNKNOWN_PRAGMA => false,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user