Permalink
Browse files

Merge branch 'release/1.0.0'

  • Loading branch information...
2 parents 5934fd8 + b30d938 commit 6c752e195594055d80dfeb8af6ae5a75d3c779e7 @bobthecow committed Mar 1, 2012
View
@@ -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 @@ protected function _setOptions(array $options) {
}
$this->_pragmas = $options['pragmas'];
}
+
+ if (isset($options['throws_exceptions'])) {
+ foreach ($options['throws_exceptions'] as $exception => $value) {
+ $this->_throwsExceptions[$exception] = $value;
+ }
+ }
}
/**
@@ -189,7 +204,7 @@ public function render($template = null, $view = null, $partials = null) {
}
$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 @@ protected function _renderPragma($matches) {
$options_string = $matches['options_string'];
if (!in_array($pragma_name, $this->_pragmasImplemented)) {
- throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
+ if ($this->_throwsException(MustacheException::UNKNOWN_PRAGMA)) {
+ throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
+ } else {
+ return '';
+ }
}
$options = array();
@@ -448,7 +467,9 @@ protected function _hasPragma($pragma_name) {
*/
protected function _getPragmaOptions($pragma_name) {
if (!$this->_hasPragma($pragma_name)) {
- throw new MustacheException('Unknown pragma: ' . $pragma_name, MustacheException::UNKNOWN_PRAGMA);
+ 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 @@ protected function _getVariable($tag_name) {
$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);
View
@@ -9,73 +9,79 @@ Usage
A quick example:
- <?php
- include('Mustache.php');
- $m = new Mustache;
- echo $m->render('Hello {{planet}}', array('planet' => 'World!'));
- // "Hello World!"
- ?>
+```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}}
+```
+Hello {{name}}
+You have just won ${{value}}!
+{{#in_ca}}
+Well, ${{taxed_value}}, after taxes.
+{{/in_ca}}
+```
Along with the associated Mustache class:
- <?php
- class Chris extends Mustache {
- public $name = "Chris";
- public $value = 10000;
+```php
+<?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;
+ public function taxed_value() {
+ return $this->value - ($this->value * 0.4);
}
+ public $in_ca = true;
+}
+```
+
Render it like so:
- <?php
- $chris = new Chris;
- echo $chris->render($template);
- ?>
+```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
- class Chris {
- public $name = "Chris";
- public $value = 10000;
-
- public function taxed_value() {
- return $this->value - ($this->value * 0.4);
- }
-
- public $in_ca = true;
- }
- ?>
+```php
+<?php
+class Chris {
+ public $name = "Chris";
+ public $value = 10000;
+ public function taxed_value() {
+ return $this->value - ($this->value * 0.4);
+ }
-And render it:
+ public $in_ca = true;
+}
+```
- <?php
- $chris = new Chris;
- $m = new Mustache;
- echo $m->render($template, $chris);
- ?>
+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',
+ '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}}
+* {{normal}}
@@ -1,4 +1,5 @@
* Chris Firescythe
* 24
+* Cycling, Fishing
* Cincinnati, OH
-* Normal
+* Normal
View
@@ -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';
+ }
+}
@@ -8,7 +8,7 @@ class MustacheExceptionTest extends PHPUnit_Framework_TestCase {
protected $pickyMustache;
protected $slackerMustache;
-
+
public function setUp() {
$this->pickyMustache = new PickyMustache();
$this->slackerMustache = new SlackerMustache();
@@ -91,6 +91,34 @@ public function testGetPragmaOptionsThrowsExceptionsIfItThinksYouHaveAPragmaButI
$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 @@ public function testUnknownPragmaException() {
$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');
@@ -47,4 +61,14 @@ public function testPragmaReset() {
$this->assertEquals('>>>', $m->render('{{%UNESCAPED}}{{symbol}}'));
$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,
+ );
}

0 comments on commit 6c752e1

Please sign in to comment.