1 |
efrain |
1 |
Mustache.php
|
|
|
2 |
============
|
|
|
3 |
|
|
|
4 |
A [Mustache](http://mustache.github.com/) implementation in PHP.
|
|
|
5 |
|
|
|
6 |
[](https://packagist.org/packages/mustache/mustache)
|
|
|
7 |
[](http://travis-ci.org/bobthecow/mustache.php)
|
|
|
8 |
[](https://styleci.io/repos/569670)
|
|
|
9 |
[](https://packagist.org/packages/mustache/mustache)
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
Usage
|
|
|
13 |
-----
|
|
|
14 |
|
|
|
15 |
A quick example:
|
|
|
16 |
|
|
|
17 |
```php
|
|
|
18 |
<?php
|
|
|
19 |
$m = new Mustache_Engine;
|
|
|
20 |
echo $m->render('Hello {{planet}}', array('planet' => 'World!')); // "Hello World!"
|
|
|
21 |
```
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
And a more in-depth example -- this is the canonical Mustache template:
|
|
|
25 |
|
|
|
26 |
```html+jinja
|
|
|
27 |
Hello {{name}}
|
|
|
28 |
You have just won {{value}} dollars!
|
|
|
29 |
{{#in_ca}}
|
|
|
30 |
Well, {{taxed_value}} dollars, after taxes.
|
|
|
31 |
{{/in_ca}}
|
|
|
32 |
```
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
Create a view "context" object -- which could also be an associative array, but those don't do functions quite as well:
|
|
|
36 |
|
|
|
37 |
```php
|
|
|
38 |
<?php
|
|
|
39 |
class Chris {
|
|
|
40 |
public $name = "Chris";
|
|
|
41 |
public $value = 10000;
|
|
|
42 |
|
|
|
43 |
public function taxed_value() {
|
|
|
44 |
return $this->value - ($this->value * 0.4);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
public $in_ca = true;
|
|
|
48 |
}
|
|
|
49 |
```
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
And render it:
|
|
|
53 |
|
|
|
54 |
```php
|
|
|
55 |
<?php
|
|
|
56 |
$m = new Mustache_Engine;
|
|
|
57 |
$chris = new Chris;
|
|
|
58 |
echo $m->render($template, $chris);
|
|
|
59 |
```
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
And That's Not All!
|
|
|
63 |
-------------------
|
|
|
64 |
|
|
|
65 |
Read [the Mustache.php documentation](https://github.com/bobthecow/mustache.php/wiki/Home) for more information.
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
See Also
|
|
|
69 |
--------
|
|
|
70 |
|
|
|
71 |
* [Readme for the Ruby Mustache implementation](http://github.com/defunkt/mustache/blob/master/README.md).
|
|
|
72 |
* [mustache(5)](http://mustache.github.com/mustache.5.html) man page.
|