Handlebars Class
Handlebars is a simple template language inspired by Mustache.
This is a YUI port of the original Handlebars project, which can be found at https://github.com/wycats/handlebars.js.
Item Index
Methods
compile
-
string
-
[options]
Compiles a Handlebars template string into a function. To render the template, call the function and pass in a context object.
Parameters:
Returns:
Compiled template function.
Example:
var template = Y.Handlebars.compile('The pie of the day is {{pie}}!.');
template({pie: 'Pecan'});
// => "The pie of the day is Pecan!"
log
-
level
-
message
Logs a debugging message. Note that messages will only be logged when the handlebars module is loaded in "debug" mode.
precompile
-
string
-
[options]
Precompiles a Handlebars template string into a string of JavaScript code. This can be used to precompile a template at build time or on the server, and the resulting template can then be rendered at runtime or on the client without needing to go through a compile step.
To render a precompiled template, evaluate the code and then pass the resulting
function to Y.Handlebars.template()
to get back an executable template
function.
Parameters:
Returns:
Precompiled template code.
registerHelper
-
name
-
fn
-
[inverse=false]
Registers a helper function that will be made available to all templates.
Helper functions receive the current template context as the this
object, and
can also receive arguments passed by the template.
Parameters:
Example:
Y.Handlebars.registerHelper('linkify', function () {
return '<a href="' + Y.Escape.html(this.url) + '">' +
Y.Escape.html(this.text) + '</a>';
});
var source = '<ul>{{#links}}<li>{{{linkify}}}</li>{{/links}}</ul>';
Y.Handlebars.render(source, {
links: [
{url: '/foo', text: 'Foo'},
{url: '/bar', text: 'Bar'},
{url: '/baz', text: 'Baz'}
]
});
registerPartial
-
name
-
partial
Registers a partial that will be made available to all templates.
A partial is another template that can be used to render part of a larger template. For example, a website with a common header and footer across all its pages might use a template for each page, which would call shared partials to render the headers and footers.
Partials may be specified as uncompiled template strings or as compiled template functions.
Parameters:
Example:
Y.Handlebars.registerPartial('header', '<h1>{{title}}</h1>');
Y.Handlebars.registerPartial('footer', 'Copyright (c) 2011 by Me.');
var source = '{{> header}} <p>Mustaches are awesome!</p> {{> footer}}';
Y.Handlebars.render(source, {title: 'My Page About Mustaches'});
render
-
string
-
context
-
[options]
Compiles and renders a Handlebars template string in a single step.
If you'll be using a template more than once, it's more efficient to compile it
into a function once using compile()
, and then render it whenever you need to
by simply executing the compiled function. However, if you only need to compile
and render a template once, render()
is a handy shortcut for doing both in a
single step.
Parameters:
Returns:
Rendered template string.
Example:
Y.Handlebars.render('The pie of the day is {{pie}}!', {
pie: 'Maple Custard'
});
// => "The pie of the day is Maple Custard!"
template
-
template
Converts a precompiled template into a renderable template function.
Parameters:
-
template
FunctionPrecompiled Handlebars template function.
Returns:
Compiled template function.
Example:
<script src="precompiled-template.js"></script>
<script>
YUI().use('handlebars-base', function (Y) {
// Convert the precompiled template function into a renderable template
// function.
var template = Y.Handlebars.template(precompiledTemplate);
// Render it.
template({pie: 'Pumpkin'});
});
</script>