Skip to content

Commit 564abe1

Browse files
committed
Reworked to flow with cake 2.x and latest mustache (with autoloader)
1 parent bb72d4a commit 564abe1

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CakePHP (2.x) Mustache Plugin
2-
Mustache View helper (**originally CakePan**) that renders Mustache templates. It will also load and process partials!
2+
Mustache View helper (*originally CakePan*) that renders Mustache templates. It will also load and process partials!
33

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

View/Helper/MustacheHelper.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@
3030
*
3131
*/
3232

33-
#App::import( 'Vendor', 'Mustache', array( 'file' => 'mustache' . DS . 'Mustache.php') );
34-
35-
App::import('Vendor', 'Mustache.Mustache',
36-
array(
37-
'file' => 'mustache' . DS . 'Mustache.php'
38-
)
39-
);
40-
4133
class MustacheHelper extends AppHelper {
4234
var $ext = 'mustache'; //Extention for the templates. 'mustache' unless noted otherwise
4335
var $partials = array(); //recursively loaded partials. Save as class variable so we don't need to double-load
4436

37+
public function __construct(View $View, $options = array()) {
38+
parent::__construct($View, $options);
39+
// include the mustache namespace autoloader
40+
App::import('Vendor', 'Mustache.mustache/src/Mustache/Autoloader');
41+
Mustache_Autoloader::register();
42+
$this->m = new Mustache_Engine;
43+
}
4544

4645
/** Returns the rendered template as HTML.
4746
* All variables should be 'set' by the CakePHP Controller
@@ -51,15 +50,12 @@ class MustacheHelper extends AppHelper {
5150
* @return string - HTML from the Mustache template
5251
*/
5352
function render( $element, $values = array() ) {
53+
5454
try {
5555
// get the template text. Also recursively loads all partials
56-
$template = $this->_loadTemplate( $element );
57-
58-
// Instantiate Mustache, with all data passed in.
59-
$M = new Mustache( $template, am($this->_View->viewVars, $values), $this->partials );
60-
56+
$template = $this->_loadTemplate($element);
6157
// generate the HTML
62-
$result = $M->render();
58+
$result = $this->m->render($template, am($this->_View->viewVars, $values), $this->partials);
6359

6460
} catch ( Exception $e ) {
6561
debug( $e );
@@ -105,7 +101,9 @@ function getSingleTemplate( $element ) {
105101
*/
106102
private function _getElementPath( $element ) {
107103
$element = str_replace('__', '/', $element);
108-
return ROOT . DS . 'app' . DS . 'View' . DS . 'Elements' . DS . $element . '.' . $this->ext;
104+
$app_path = App::path('View');
105+
$app_path = $app_path[0];
106+
return $app_path . DS . 'Elements' . DS . $element . '.' . $this->ext;
109107
}
110108

111109

0 commit comments

Comments
 (0)