Giter VIP home page Giter VIP logo

komust's Introduction

Komust

Komust is a module for Kohana, that integrates Mustache templates (using mustache.php implementation.

Examples

There are 2 ways to use Komust:

Instead of extending the default Kohana Controller, extend Controller_Komust.

<?php
class Controller_Welcome extends Controller_Komust {
	public function action_index()
	{
		$this->layout = 'main';
		$this->response->body($this->render('welcome/index',array('name' => 'World')));
	}
}

The render method in Controller_Komust, renders welcome/index with the provided data and adds the result back to the data as property/array element "content", so the rendered view can be accessed by the layout template by {{{content}}}.

Now, the only thing remaining is to create the layout and the actual view templates. Layout template (layout.mustache) could be something like:

<!DOCTYPE html>
<html>
	<head>
	</head>
	<body>
		{{{content}}}
	</body>
</html>

And welcome/index(.mustache) could look like:

<div>
	Hello {{name}}
</div>

That's it! Of course, in real world it is more efficient and the controller code is more readable if you use ViewModels. Let's refactor our code to do so:

<?php
class Controller_Welcome extends Controller_Komust {
	public function action_index()
	{
		$this->layout = 'main';
		$this->response->body($this->render('welcome/index', View_Komust::factory('Welcome/Index')));
	}
}

Now, we can add the data we need to be available in our template in the View_Komust class. Let's see the class' contents:

<?php
class View_Welcome_Index {
	public $name = 'World';
}

Our template remains the same, so no changes are needed there. That's it! The good thing about ViewModels is that you can add methods which are also available in your templates!

The second approach one may choose is to create a regular controller and render a template (without all the automagic) by using the render method of the Mustache_Engine:

<?php
class Controller_Welcome extends Controller {
	public function action_index()
	{
		$mustache = Komust::factory()->engine();
		$this->response->body($mustache->render('{{message}}',array('message' => 'Hello World!')));
	}
}

The default factory call uses the default settings of Mustache_Engine (string loader), but you can customize that to use filesystem loader which can accomodate more complex needs. The factory method accepts a $config parameter, either an array (which holds the configuration settings) or a string, which is the config group of the komust.php config file (you can add your own configurations there). If $config is not provided, a Mustache_Engine instance with the default settings is created.

komust's People

Contributors

m1lt0n avatar pvratsalis avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.