Skip to content

Commit 1c00b9f

Browse files
committed
Merge branch 'cakephp-2.x'
2 parents 72dbca3 + 5d50467 commit 1c00b9f

File tree

3 files changed

+70
-44
lines changed

3 files changed

+70
-44
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
# Ignore the other helpers. Only pay attention to mustache.php
55
*.php
6-
!mustache.php
6+
!MustacheHelper.php

mustache.php renamed to MustacheHelper.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,15 @@ class MustacheHelper extends AppHelper {
4444
* @param array $values - passed in values that are merged with the view variables. Associative array
4545
* @return string - HTML from the Mustache template
4646
*/
47-
function element( $element, $values = array() ) {
47+
function render( $element, $values = array() ) {
4848
try {
4949
// get the template text. Also recursively loads all partials
5050
$template = $this->_loadTemplate( $element );
51-
52-
// grab the Cake view with all variables
53-
$V = ClassRegistry::getObject('view');
54-
51+
5552
// Instantiate Mustache, with all data passed in.
56-
$M = new Mustache( $template, am( $V->viewVars, $values), $this->partials );
53+
$M = new Mustache( $template, am($this->_View->viewVars, $values), $this->partials );
5754

58-
//generate the HTML
55+
// generate the HTML
5956
$result = $M->render();
6057

6158
} catch ( Exception $e ) {
@@ -102,7 +99,7 @@ function getSingleTemplate( $element ) {
10299
*/
103100
private function _getElementPath( $element ) {
104101
$element = str_replace('__', '/', $element);
105-
return ROOT . DS . 'app' . DS . 'views' . DS . 'elements' . DS . $element . '.' . $this->ext;
102+
return ROOT . DS . 'app' . DS . 'View' . DS . 'Elements' . DS . $element . '.' . $this->ext;
106103
}
107104

108105

@@ -116,7 +113,7 @@ private function _loadTemplate( $element, $load_sub_templates = true ) {
116113

117114
//fail nicely if we have a bad file
118115
if(!file_exists( $path ) ) {
119-
debug( "Bad template path: $element<br />" );
116+
debug( "Bad template path: $path" );
120117
return '';
121118
}
122119

@@ -139,7 +136,7 @@ private function _loadTemplate( $element, $load_sub_templates = true ) {
139136
*/
140137
private function _loadPartials( $template ) {
141138
//Extract names of any partials from the template
142-
preg_match_all( '/\{\{[\s]*\>[\s]*(.*)[\s]*\}\}/', $template, $partials );
139+
preg_match_all( '/\{\{\> (\S+)\}\}/', $template, $partials );
143140

144141
// iterate through the partials
145142
// adds the corresponding templates to the partials list while avoiding duplicates
@@ -150,4 +147,4 @@ private function _loadPartials( $template ) {
150147
}
151148
}
152149
}
153-
}
150+
}

README.md

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,78 @@
1-
#CakePan
1+
# CakePan - CakePHP Mustache Support (CakePHP v2.x)
22

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!
44

55
### Why use Mustache templates in CakePHP?
66
<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.
77

88
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.
99

10-
## Use
10+
## Installation
1111

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.
1415

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.
1617

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`
1919

20-
The Mustache manual is here: http://mustache.github.com/
20+
class AppController extends Controller {
21+
...
22+
public $helpers = array('Mustache');
23+
...
24+
}
2125

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
2427

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/)
2629

27-
<pre>
28-
<strong>/views/elements/posts/post.mustache :</strong>
29-
{{#Post}}
30-
&lt;h2&gt;{{title}}&lt;/h2\&gt;
31-
&lt;div&gt;
32-
{{text}}
33-
&lt;/div&gt;
34-
{{/Post}}
35-
{{#Comment}}
36-
{{&gt;post/comment}}
37-
{{/Comment}}
30+
### Creating a Mustache Template
3831

32+
Your Mustache templates should all be in the `/app/View/Elements/` directory, with a `.mustache` extension.
3933

40-
<strong>/views/elements/posts/comment.mustache :</strong>
41-
&lt;div&gt;
42-
&lt;h3&gt;{{#User}}{{name}}{{/User}} said: &lt;/h3&gt;
43-
&lt;p&gt;{{text}}&lt;/p&gt;
44-
&lt;/div&gt;
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
4735

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

Comments
 (0)