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.
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:
-
string
StringHandlebars template string to compile.
-
[options]
Object optionalCompiler options.
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.
Parameters:
-
level
StringLog level for this message. Supported levels are "debug", "info", "warn", and "error".
-
message
StringMessage to log.
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:
-
string
StringHandlebars template string to compile.
-
[options]
Object optionalCompiler options.
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:
-
name
StringName of this helper.
-
fn
FunctionHelper function.
-
[inverse=false]
Boolean optionalIf
true
, this helper will be considered an "inverse" helper, like "unless". This means it will only be called if the expression given in the template evaluates to a false or empty value.
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:
-
name
StringName of this partial.
-
partial
Function | StringTemplate string or compiled template function.
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:
-
string
StringHandlebars template string to render.
-
context
ObjectContext object to pass to the template.
-
[options]
Object optionalCompile/render options.
-
[helpers]
Object optionalHelper functions.
-
[partials]
Object optionalPartials.
-
[data]
Object optionalData.
-
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>