Yii 1.1: mustache

Easy mustache templating with Yii
12 followers

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.

Total 6 comments

#16461 report it
davey` at 2014/02/26 06:11am
Sorry for not answering

I'm not really maintening this anymore. I'll try to update it if I have time or rewrite it for yii2.

Your suggestion sounds about right though.

About yii's t(), I think you need to write different templates for each language and maybe use a suffix (en, de, fr...) in the file name.

To pass templates to JavaScript you can create a PHP array like:

$templates = array (
    'typical' => Yii::app()->mustache->getTemplate('typical');
)

then convert this array to JSON with CJSON::encode() and pass it as a JS variable with a registerScript() call.

#16124 report it
Fire at 2014/01/21 10:59pm
How do we pass the raw templates to Javascript

Hi Davey,

You mentioned that using

Yii::app()->mustache->getTemplate('typical');

we can get the raw template and easily pass it to javascript. How would we do this in yii?

#16115 report it
Fire at 2014/01/21 08:04am
is there a way to use yii's t() function so I can localize language in my js templates?

is there a way to use yii's t() function so I can localize language in my js templates?

#16109 report it
Fire at 2014/01/21 01:16am
Suggested Modification

Hi everyone, I think this module would be better if we were to modify line 28 in /protected/extensions/mustache/components/MustacheApplicationComponent.php

from

echo $this->m->render($this->getTemplate($templateName), $view, $partials, $options);

to

return $this->m->render($this->getTemplate($templateName), $view, $partials, $options);

That way, you could use this function in places other than just views.

Ie:

$s= Yii::app()->mustache->render('testing', $typical_data);
        echo $s;
#7019 report it
davey` at 2012/02/19 05:12pm
Thanks!

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!

#7018 report it
allain at 2012/02/19 04:55pm
nice

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 to leave your comment.

Create extension
Downloads