This extension is a wrapper for Mustache.php that provides easy mustache templating for Yii.
Mustache is useful if you need a uniform templating language server-side and client-side.
Tested with Yii 1.1.9 and above.
Extract the files in protected/extensions/mustache and add this to your app configuration:
// application components 'components'=>array( ... 'mustache'=>array( 'class'=>'ext.mustache.components.MustacheApplicationComponent', // Default settings (not needed) 'templatePathAlias'=>'application.templates', 'templateExtension'=>'mustache', 'extension'=>true, ), ... ),
Create a "templates" directory under protected. Create in this dir a "typical.mustache" file with these contents:
Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}
In your view, paste this code:
$typical_data = array ( "name" => "Chris", "value" => 10000, "taxed_value" => 10000 - (10000 * 0.4), "in_ca" => true ); Yii::app()->mustache->render('typical', $typical_data);
You should get this output:
Hello Chris You have just won $10000! Well, $6000, after taxes.
You can get the raw template by calling:
Yii::app()->mustache->getTemplate('typical');
So you can easily pass it to your javascript code or your JQuery plugins, as well as your JSON-encoded data and render it with hogan.js or mustache.js.
Here is a complete example that shows what you can do with partials.
The PHP view:
$message = array ( "from" => "anna", "to" => array ( array ( "name" => "john" ), array ( "name" => "bob" ), array ( "name" => "claire" ), ), "hasCC" => false, "content" => "Hi, will you attend my birthday party?", // We need a boolean in case of recursive partials // see: https://github.com/bobthecow/mustache.php/issues/44 "hasReplies" => true, "replies" => array ( array ( "from" => "john", "to" => array ( array ( "name" => "anna" ), ), "hasCC" => true, "cc" => array ( array ( "name" => "bob" ), array ( "name" => "claire" ), ), "content" => "Of course!", "hasReplies" => true, "replies" => array ( "from" => "claire", "to" => array ( array ( "name" => "anna" ), array ( "name" => "john" ), array ( "name" => "claire" ), ), "hasCC" => true, "cc" => array ( array ( "name" => "bob" ), ), "content" => "Let's party like it's 1999!", "hasReplies" => false, ), ), array ( "from" => "bob", "to" => array ( array ( "name" => "anna" ), ), "hasCC" => false, "content" => "Sorry I can't :(", "hasReplies" => false, ), ), ); // Partials have to be passed as the third parameter. // In the case of recursive partials we have to pass // our template twice. It's odd but it's the way it works. Yii::app()->mustache->render('message', $message, array('message'));
The message.mustache template:
<div style="margin: 0px 5px 5px 50px; border-style: solid; border-width:1px;">
<b>from:</b> {{from}}<br/>
<b>to:</b>
<ul>
{{#to}}
<li>{{name}}</li>
{{/to}}
</ul>
<b>cc:</b>
{{#hasCC}}
<ul>
{{#cc}}
<li>{{name}}</li>
{{/cc}}
</ul>
{{/hasCC}}
{{^hasCC}}
None <br/>
{{/hasCC}}
<b>Content:</b> {{content}}<br/>
{{#hasReplies}}
<b>Replies:</b>
{{#replies}}{{>message}}{{/replies}}
{{/hasReplies}}
</div>
And the HTML output:
To insert dynamic URLs in templates you will have to pass them in your view data. But for static URLs you can use the following in your templates:
%%controller/action%% will generate Yii::app()->createUrl(<controller/action>) %%%relativePath%%% will generate Yii::app()->request->baseUrl . '/' . <relativePath>
This can be disabled if you set the extension property to false.
Total 2 comments
You might be right about putting templates in views rather than in their own directory.
I'll think about it and change it if needed.
Thanks for the feedback!
too bad it count be implemented to support the normal render and renderPartial methods.
also, having templates as a separate directory than views is practically unnecessary since the extension tells you what template language you are using.
having said the above, great extension I will be using it regardless.
Thanks
Leave a comment
Please login to leave your comment.