Git Product home page Git Product logo

emustache's Introduction

#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

##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

emustache's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.