Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Mar 1, 2012
2 parents 5934fd8 + b30d938 commit 6c752e1
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 53 deletions.
31 changes: 26 additions & 5 deletions Mustache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class Mustache {

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

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
90 changes: 48 additions & 42 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions examples/dot_notation/DotNotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion examples/dot_notation/dot_notation.mustache
Original file line number Diff line number Diff line change
@@ -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}}
3 changes: 2 additions & 1 deletion examples/dot_notation/dot_notation.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
* Chris Firescythe
* 24
* Cycling, Fishing
* Cincinnati, OH
* Normal
* Normal
24 changes: 24 additions & 0 deletions test/MustacheCallTest.php
Original file line number Diff line number Diff line change
@@ -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';
}
}
36 changes: 34 additions & 2 deletions test/MustacheExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -117,4 +145,8 @@ class TestableMustache extends Mustache {
public function testableGetPragmaOptions($pragma_name) {
return $this->_getPragmaOptions($pragma_name);
}
}

public function getThrownExceptions() {
return $this->_throwsExceptions;
}
}
24 changes: 24 additions & 0 deletions test/MustachePragmaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.