Handlebars is a logic-less templating engine that dynamically generates your HTML page. It’s an extension of Mustache with a few additional features
A Handlebars template is just some text that contains Handlebars expressions like {{foo}}. Although most people use Handlebars to generate HTML, it can easily generate JSON, XML, YAML
, or any other plain text format.
When you render a template, Handlebars evaluates expressions and replaces them with data
The variables used inside the templates are surrounded by double curly braces {{}}
and are known as expressions
{
firstname: "Yehuda",
lastname: "Katz",
}
#OUTPUT
Yehuda Katz
<p>Yehuda Katz</p>
<script id="list-template" type="text/x-handlebars-template">
<p>YUI is brought to you by:</p>
<ul>
{{#items}}
<li><a href="{{url}}">{{name}}</a></li>
{{/items}}
</ul>
</script>
<div class="header">
{{#site}}
<h1><a href="{{url}}">{{title}}</a></h1>
{{/site}}
</div>
<div class="content">
{{#article}}
<h2>{{title}}</h2>
{{/article}}
</div>
{
site: {
title: 'AwesomeBlog',
url: 'http://blog.example.com'
},
article: {
id: 1,
title: 'My blog is awesome'
}
}
<script>
YUI().use('handlebars', 'node-base', function (Y) {
// Extract the template string and compile it into a reusable function.
var source = Y.one('#list-template').getHTML(),
template = Y.Handlebars.compile(source),
html;
// Render the template to HTML using the specified data.
html = template({
items: [
{name: 'pie', url: 'http://pieisgood.org/'},
{name: 'mountain dew', url: 'http://www.mountaindew.com/'},
{name: 'kittens', url: 'http://www.flickr.com/search/?q=kittens'},
{name: 'rainbows', url: 'http://www.youtube.com/watch?v=OQSNhk5ICTI'}
]
});
// Append the rendered template to the page.
Y.one('body').append(html);
});
</script>
YUI is brought to you by:
pie
mountain dew
kittens
rainbows
In Handlebars, the values returned by the {{expression}}
are HTML-escaped. Say, if the expression contains &
, then the returned HTML-escaped output is generated as &
;. If you don't want Handlebars to escape a value, use the "triple-stash"
, {{{
var context = {
"language" : "<h3>Handlebars</h3>",
"adjective": "<h3>awesome</h3>"
}
the resulting HTML will be:
I am learning <h3>Handlebars</h3>. It is <h3>awesome</h3>
- {{ }} this will render a plain text
- {{{ }}} this will everything even if we a special character. This is a great example for password
Handlebars provides several built-in block helpers that are always available to templates.
The each helper iterates over an array. The block will be rendered once for each item, and its context will be set to the current item.
The with block helper renders the given block in a different context. This can save you some typing in a template that will render a lot of namespaced data.
if here
If we empty the users array and re-render the template, the list won't be included in the output.
We can add an else
block to display an informative message when no users are online.
The unless
block helper does exactly the opposite of the if
helper: it renders the contents of the block if the provided value is falsy or an empty array.