mustache Easy mustache templating with Yii

  1. mustache extension v0.1.0
  2. Requirements
  3. Setup
  4. Usage
  5. Complete example
  6. Insert URLs in templates

mustache extension v0.1.0

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.

Requirements

Tested with Yii 1.1.9 and above.

Setup

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,
		),

		...

	),

Usage

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.

Complete example

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:

from: anna
to:
  • john
  • bob
  • claire
cc: None
Content: Hi, will you attend my birthday party?
Replies: from: john
to:
  • anna
cc:
  • bob
  • claire
Content: Of course!
Replies: from: claire
to:
  • anna
  • john
cc:
  • bob
Content: Let's party like it's 1999!
from: bob
to:
  • anna
cc: None
Content: Sorry I can't :(

Insert URLs in templates

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.

7 0
10 followers
772 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: User Interface
Developed by: davey`
Created on: Feb 19, 2012
Last updated: 12 years ago

Downloads

show all

Related Extensions