yui-handlebars-base-after.js revision 303c0f451e22f69b62c43a65066c6c20d27fcd63
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright// This file contains YUI-specific wrapper code and overrides for the
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright// handlebars-base module.
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek GathrightHandlebars is a simple template language inspired by Mustache.
7a6ad76d2e039a8cbf25b4a0babafef7bbd54c01Derek GathrightThis is a YUI port of the original Handlebars project, which can be found at
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright@module handlebars
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright@main handlebars
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek GathrightProvides basic Handlebars template rendering functionality. Use this module when
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathrightyou only need to render pre-compiled templates.
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright@module handlebars
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright@submodule handlebars-base
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek GathrightHandlebars is a simple template language inspired by Mustache.
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek GathrightThis is a YUI port of the original Handlebars project, which can be found at
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright@class Handlebars
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright// The rest of this file is just API docs for methods defined in Handlebars
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek GathrightRegisters a helper function that will be made available to all templates.
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek GathrightHelper functions receive the current template context as the `this` object, and
efa2177e01a423054231b00ca0e3c518e7ec79b9Derek Gathrightcan also receive arguments passed by the template.
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright Y.Handlebars.registerHelper('linkify', function () {
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright return '<a href="' + Y.Escape.html(this.url) + '">' +
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright Y.Escape.html(this.text) + '</a>';
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright var source = '<ul>{{#links}}<li>{{{linkify}}}</li>{{/links}}</ul>';
efa2177e01a423054231b00ca0e3c518e7ec79b9Derek Gathright Y.Handlebars.render(source, {
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright {url: '/foo', text: 'Foo'},
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright {url: '/bar', text: 'Bar'},
efa2177e01a423054231b00ca0e3c518e7ec79b9Derek Gathright {url: '/baz', text: 'Baz'}
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright@method registerHelper
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright@param {String} name Name of this helper.
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright@param {Function} fn Helper function.
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright@param {Boolean} [inverse=false] If `true`, this helper will be considered an
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright "inverse" helper, like "unless". This means it will only be called if the
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright expression given in the template evaluates to a false or empty value.
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek GathrightRegisters a partial that will be made available to all templates.
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek GathrightA partial is another template that can be used to render part of a larger
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathrighttemplate. For example, a website with a common header and footer across all its
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathrightpages might use a template for each page, which would call shared partials to
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathrightrender the headers and footers.
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek GathrightPartials may be specified as uncompiled template strings or as compiled template
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright Y.Handlebars.registerPartial('header', '<h1>{{title}}</h1>');
6aaa468f50af23f08685835d1984767b81a4446aDerek Gathright Y.Handlebars.registerPartial('footer', 'Copyright (c) 2011 by Me.');
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright var source = '{{> header}} <p>Mustaches are awesome!</p> {{> footer}}';
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright Y.Handlebars.render(source, {title: 'My Page About Mustaches'});
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright@method registerPartial
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright@param {String} name Name of this partial.
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright@param {Function|String} partial Template string or compiled template function.
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek GathrightConverts a precompiled template into a renderable template function.
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright <script src="precompiled-template.js"></script>
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright YUI().use('handlebars-base', function (Y) {
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright // Convert the precompiled template function into a renderable template
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright var template = Y.Handlebars.template(precompiledTemplate);
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright // Render it.
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright template({pie: 'Pumpkin'});
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright@method template
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright@param {Function} template Precompiled Handlebars template function.
f8c91a3a8c718916f48f0f8260c0ad5d565f8edaDerek Gathright@return {Function} Compiled template function.