Hello World

This is a basic example to use mustache or handlebars template.

Define the template

lightncandy

$template = 'Hello, {{foo}}!';

handlebars.js

var template = 'Hello, {{foo}}!';

mustache

var template = 'Hello, {{foo}}!';

Compile the template

lightncandy

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);

handlebars.js

var render = Handlebars.compile(template);

mustache

mustache.js supports pre-parsing. The parsed token tree will be cached.
Mustache.parse(template);

Define the data

lightncandy

$data = array('foo' => 'world');

handlebars.js

var data = {foo: 'world'};

mustache

var data = {foo: 'world'};

Rendering the template

lightncandy

echo $render($data);

handlebars.js

console.log(render(data));

mustache

console.log(Mustache.render(template, data));

Reuse the render function

lightncandy

echo $render($another_data);

handlebars.js

console.log(render(another_data));

mustache

console.log(Mustache.render(template, another_data));

The full code

lightncandy

Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;

// Define the template
$template = 'Hello, {{foo}}!';

// Compile the template
$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);

// Define the data
$data = array('foo' => 'world');

// Rendering the template
echo $render($data);

Output

Hello, world!

handlebars.js

Source Code
var Handlebars = require('handlebars');

// Define the template
var template = 'Hello, {{foo}}!';

// Compile the template
var render = Handlebars.compile(template);

// Define the data
var data = {foo: 'world'};

// Rendering the template
console.log(render(data));

Output

Hello, world!

mustache

Source Code
var Mustache = require('mustache');

// Define the template
var template = 'Hello, {{foo}}!';

// Compile the template
Mustache.parse(template);

// Define the data
var data = {foo: 'world'};

// Rendering the template
console.log(Mustache.render(template, data));

Output

Hello, world!