|
1 | | -#CakePan |
| 1 | +# CakePan - CakePHP Mustache Support (CakePHP v2.x) |
2 | 2 |
|
3 | | -CakePan is a CakePHP view helper that renders Mustache templates. It will also load and process sub-templates! |
| 3 | +CakePan is a CakePHP view helper that renders Mustache templates. It will also load and process partials! |
4 | 4 |
|
5 | 5 | ### Why use Mustache templates in CakePHP? |
6 | 6 | <strong>Portability and scalability!</strong> If you have an app that uses lots of front-end coding, you only have to write your templates once. Mustache templates can be rendered in PHP, Javascript, Ruby, Scala, even C++! If you want to move to or from some other framework (Rails, Grails, Lithium etc.), you can be sure that your views and design won't have to be re-built. |
7 | 7 |
|
8 | 8 | For scalability, when the time comes, you can use templates with a more powerful engine like Scala, or just send JSON from any source, and render with Javascript. |
9 | 9 |
|
10 | | -## Use |
| 10 | +## Installation |
11 | 11 |
|
12 | | -### install |
13 | | -First - make afolder called 'mustache' in your vendors folder. Add the Mustache PHP library to your app/vendor folder. from https://github.com/bobthecow/mustache.php/ |
| 12 | +### 1. Add the [PHP Mustache library](https://github.com/bobthecow/mustache.php/) to `app/vendors/mustache` |
| 13 | +### 2. Add CakePan's `MustacheHelper.php` file to `app/Views/Helpers`. |
| 14 | +### 3. Add the Mustache View Helper to your pages. |
14 | 15 |
|
15 | | -Then, place this helper out into the views/helpers folder of your CakePHP project. |
| 16 | +CakePan should be added to your project the same as any other CakePHP View Helper. See the [Cakebook's View Helpers documentation](http://book.cakephp.org/2.0/en/views/helpers.html) for more information. |
16 | 17 |
|
17 | | -### production |
18 | | -Write your elements in mustache format with the extension ".mustache" rather than ".ctp"! |
| 18 | +If you want to add Mustache support globally, add it to your `AppController` |
19 | 19 |
|
20 | | -The Mustache manual is here: http://mustache.github.com/ |
| 20 | + class AppController extends Controller { |
| 21 | + ... |
| 22 | + public $helpers = array('Mustache'); |
| 23 | + ... |
| 24 | + } |
21 | 25 |
|
22 | | -In your view - render an element using $this->Mustache->render('element', $params); just like you would render a CakePHP element. |
23 | | -All the variable set by the controller are available, and merged with values passed into $params. |
| 26 | +## Usage |
24 | 27 |
|
25 | | -Sub-templates should follow the same naming convention. Mustache will pass the variables to the sub-template in the context that it's called. For example, a nested template for a blog post with comments might look like: |
| 28 | +See the Mustache manual: [http://mustache.github.com/](http://mustache.github.com/) |
26 | 29 |
|
27 | | -<pre> |
28 | | -<strong>/views/elements/posts/post.mustache :</strong> |
29 | | -{{#Post}} |
30 | | - <h2>{{title}}</h2\> |
31 | | - <div> |
32 | | - {{text}} |
33 | | - </div> |
34 | | -{{/Post}} |
35 | | -{{#Comment}} |
36 | | - {{>post/comment}} |
37 | | -{{/Comment}} |
| 30 | +### Creating a Mustache Template |
38 | 31 |
|
| 32 | +Your Mustache templates should all be in the `/app/View/Elements/` directory, with a `.mustache` extension. |
39 | 33 |
|
40 | | -<strong>/views/elements/posts/comment.mustache :</strong> |
41 | | -<div> |
42 | | -<h3>{{#User}}{{name}}{{/User}} said: </h3> |
43 | | -<p>{{text}}</p> |
44 | | -</div> |
45 | | -</pre> |
46 | | -In this example, the post/comment element is called within the context of one of the comments (which in this case belongs to a User) |
| 34 | +/app/View/Elements/post.mustache |
47 | 35 |
|
48 | | -## Todo |
49 | | -- Test suite |
| 36 | + {{#Post}} |
| 37 | + <h2>{{title}}</h2\> |
| 38 | + <div> |
| 39 | + {{text}} |
| 40 | + </div> |
| 41 | + {{/Post}} |
| 42 | + |
| 43 | + |
| 44 | +### Rendering a Mustache Template |
| 45 | + |
| 46 | +All the variable set by the controller are available, and merged with values passed into `$params`. |
| 47 | + |
| 48 | + $params = array( |
| 49 | + 'title' => 'Show me the bacon!', |
| 50 | + 'text' => 'Bacon ipsum dolor sit amet fatback pig swine...' |
| 51 | + ); |
| 52 | + |
| 53 | + $this->Mustache->render('template_name', $params) |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +### Using Partials |
| 58 | + |
| 59 | +Partials should follow the same naming convention. Mustache will pass the variables to the partial in the context that it's called. For example, a nested template for a blog `post` with `comments` might look like: |
| 60 | + |
| 61 | +/app/View/Elements/posts/post.mustache: |
| 62 | + |
| 63 | + {{#Post}} |
| 64 | + <h2>{{title}}</h2\> |
| 65 | + <div> |
| 66 | + {{text}} |
| 67 | + </div> |
| 68 | + {{/Post}} |
| 69 | + {{#Comment}} |
| 70 | + {{>post/comment}} |
| 71 | + {{/Comment}} |
| 72 | + |
| 73 | +/app/View/Elements/posts/comment.mustache: |
| 74 | + |
| 75 | + <div> |
| 76 | + <h3>{{#User}}{{name}}{{/User}} said: </h3> |
| 77 | + <p>{{text}}</p> |
| 78 | + </div> |
0 commit comments