Yii 1.1: emustache

Mustache templating for Yii
9 followers

EMustache - Mustache templating for Yii

This extension is based on the excellent work of Justin Hileman aka. "bobthecow" => mustache.php at https://github.com/bobthecow/mustache.php The idea was to make mustache templating with Yii as easy as possible. It therefore uses the familiar Yii rendering syntax. In order to make this extension as fast as possible EMustache uses Mustache.php's built in caching functionality. By default these cached views are stored under /protected/runtime/Mustache/cache

The project is ONLY downloadable at Github!

EMustache on Github

Requirements

  • Tested with Yii 1.1.11 but should work for lower versions too
  • PHP 5.3 or above recommended

Usage

INSTALL:

1.) Add the extension to Yii by placing it in your application's extension folder (for example '/protected/extensions') 2.) Add the viewRenderer configuration to the main config (within the "components section")

'viewRenderer' => array(
    'class' => 'application.extensions.EMustache.EMustacheViewRenderer',
    )

3.) Add additional options. To pass mustache.php specific options you may use the following syntax. Example:

'viewRenderer' => array(
   'class' => 'application.extensions.EMustache.EMustacheViewRenderer',
        'mustacheOptions'=>array(
            'charset' => Yii::app()->charset
        )
   )

Please note that the options "partial_loader","charset" and "escape" are already set to make Mustache.php usable with Yii's conventions. E.g.: Escaping is done via Yii's CHtml::encode() and views/partials are searched within "protected/views". Only change them if you know what you do.

USAGE:

With layouts:

protected/layouts/main.mustache:

<!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <div class="container" id="page">
            {{{content}}}
        </div><!-- page -->
    </body>
</html>

protected/views/site/index.mustache:

Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}

protected/controllers/SiteController.php

<?php

class SiteController extends Controller
{   
    public function actionIndex()
    {
        $this->layout="//layouts/main";
        $this->render('index',array(
                    'name'=>'Chris',
                    'value'=>10000,
                    'taxed_value'=>10000 - (10000 * 0.4),'in_ca'=>true)
                );
    }
}

http://localhost/mustacheExample/index.php will output:

<!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <div class="container" id="page">
            Hello Chris
            You have just won $10000!
            Well, $6000.0, after taxes.
        </div><!-- page -->
    </body>
</html>

Partials:

protected/views/site/index.mustache:

Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}
And here comes a partial 
{{>site/partial}}

protected/views/site/partial.mustache:

I am a partial with name {{partialName}} and you are probably named {{name}}

Partials will automatically have access to variables of their parents.

<?php
class SiteController extends Controller
{   
    public function actionIndex()
    {
        $this->layout="//layouts/main";
        $this->render('index',array(
                'name'=>'Chris',
                'value'=>10000,
                'taxed_value'=>10000 - (10000 * 0.4),
                'in_ca'=>true,
                'partialName'=>'IAMAPARTIAL'));
    }
}

Using models

<?php
class SiteController extends Controller
{   
    public function actionIndex()
    {
        $model=new YiiModel;
                $model->name='Haensel;
                $model->age=30;
                $this->render('index',array('model'=>$model);
    }
}

protected/views/site/index.mustache:

Name: {{model.name}}
Age: {{model.age}}

Full mustache syntax: http://mustache.github.com/mustache.5.html

Total 3 comments

#10808 report it
ajsharma at 2012/11/25 10:02pm
Gii

Probably too much to hope for, but have you worked on making Gii generators at all?

#9614 report it
Haensel at 2012/08/29 03:36am
widget support

@gsoul

Mustache is a logicless templating system so can't use widgets in a template. But that's not a flaw, it is actually ment to not allow that. If you need complex logic then you might be interested in "lambdas" (They are explained here: https://github.com/bobthecow/mustache.php/wiki/Mustache-Tags) or in passing objects to a template that output the html (e.g.: {{object.method}}). But be careful not to "destroy" the real benefit of mustache: reusability. Because there is no logic you actually can make your templates available on the server-side and client-side (with mustache.js). That's what I am using it for. If you are building a quite "server-side heavy" app and don't need reusable templates then mustache is overkill

cheers,

Hannes

#9610 report it
Cherif at 2012/08/28 06:24pm
Widgets

Nice work, is there a way to use Yii widgets with EMustach?

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet