2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('template-base', function (Y, NAME) {
11 Virtual rollup of the `template-base` and `template-micro` modules.
19 Provides a generic API for using template engines such as Handlebars and
23 @submodule template-base
28 Provides a generic API for using template engines such as Handlebars and
33 Using with `Y.Template.Micro` (the default template engine):
35 YUI().use('template', function (Y) {
36 var micro = new Y.Template(),
37 html = micro.render('<%= data.message %>', {message: 'hello!'});
42 Using with Handlebars:
44 YUI().use('template-base', 'handlebars', function (Y) {
45 var handlebars = new Y.Template(Y.Handlebars),
46 html = handlebars.render('{{message}}', {message: 'hello!'});
52 @param {Mixed} [engine=Y.Template.Micro] Template engine to use, such as
53 `Y.Template.Micro` or `Y.Handlebars`. Defaults to `Y.Template.Micro` if not
55 @param {Object} [defaults] Default options to use when instance methods are
61 function Template(engine, defaults) {
65 @property {Object} defaults
68 this.defaults = defaults;
71 Template engine class.
73 @property {Mixed} engine
76 this.engine = engine || Y.Template.Micro;
79 Y.error('No template engine loaded.');
84 Registry that maps template names to revived template functions.
92 Template._registry = {};
95 Registers a pre-compiled template into the central template registry with a
96 given template string, allowing that template to be called and rendered by
97 that name using the `Y.Template.render()` static method.
99 For example, given the following simple Handlebars template, in `foo.hbs`:
103 It can be precompiled using the Handlebars CLI, and added into a YUI module
104 in the following way. Alternatively, `locator` can be used to automate this
107 YUI.add('templates-foo', function (Y) {
109 var engine = new Y.Template(Y.Handlebars),
112 precompiled = // Long precompiled template function here //
114 Y.Template.register('foo', engine.revive(precompiled));
116 }, '0.0.1', {requires: ['template-base', 'handlebars-base']});
118 See the `Y.Template#render` method to see how a registered template is used.
121 @param {String} templateName The template name.
122 @param {Function} template The function that returns the rendered string. The
123 function should take the following parameters. If a pre-compiled template
124 does not accept these parameters, it is up to the developer to normalize it.
125 @param {Object} [template.data] Data object to provide when rendering the
127 @param {Object} [template.options] Options to pass along to the template
128 engine. See template engine docs for options supported by each engine.
129 @return {Function} revivedTemplate This is the same function as in `template`,
130 and is done to maintain compatibility with the `Y.Template#revive()` method.
134 Template.register = function (templateName, template) {
135 Template._registry[templateName] = template;
140 Returns the registered template function, given the template name. If an
141 unregistered template is accessed, this will return `undefined`.
144 @param {String} templateName The template name.
145 @return {Function} revivedTemplate The revived template function, or `undefined`
146 if it has not been registered.
151 Template.get = function (templateName) {
152 return Template._registry[templateName];
156 Renders a template into a string, given the registered template name and data
157 to be interpolated. The template name must have been registered previously with
160 Once the template has been registered and built into a YUI module, it can be
161 listed as a dependency for any other YUI module. Continuing from the above
162 example, the registered template can be used in the following way:
165 YUI.add('bar', function (Y) {
167 var html = Y.Template.render('foo', {
168 tagline: '"bar" is now template language agnostic'
171 }, '0.0.1', {requires: ['template-base', 'templates-foo']});
173 The template can now be used without having to know which specific rendering
177 @param {String} templateName The abstracted name to reference the template.
178 @param {Object} [data] The data to be interpolated into the template.
179 @param {Object} [options] Any additional options to be passed into the template.
180 @return {String} output The rendered result.
184 Template.render = function (templateName, data, options) {
185 var template = Template._registry[templateName],
189 result = template(data, options);
191 Y.error('Unregistered template: "' + templateName + '"');
197 Template.prototype = {
199 Compiles a template with the current template engine and returns a compiled
203 @param {String} text Template text to compile.
204 @param {Object} [options] Options to pass along to the template engine. See
205 template engine docs for options supported by each engine.
206 @return {Function} Compiled template function.
209 compile: function (text, options) {
210 options = options ? Y.merge(this.defaults, options) : this.defaults;
211 return this.engine.compile(text, options);
215 Precompiles a template with the current template engine and returns a string
216 containing JavaScript source code for the precompiled template.
219 @param {String} text Template text to compile.
220 @param {Object} [options] Options to pass along to the template engine. See
221 template engine docs for options supported by each engine.
222 @return {String} Source code for the precompiled template.
225 precompile: function (text, options) {
226 options = options ? Y.merge(this.defaults, options) : this.defaults;
227 return this.engine.precompile(text, options);
231 Compiles and renders a template with the current template engine in a single
232 step, and returns the rendered result.
235 @param {String} text Template text to render.
236 @param {Object} data Data object to provide when rendering the template.
237 @param {Object} [options] Options to pass along to the template engine. See
238 template engine docs for options supported by each engine.
239 @return {String} Rendered result.
242 render: function (text, data, options) {
243 options = options ? Y.merge(this.defaults, options) : this.defaults;
245 if (this.engine.render) {
246 return this.engine.render(text, data, options);
249 return this.engine.compile(text, options)(data, options);
253 Revives a precompiled template function into an executable template function
254 using the current template engine. The precompiled code must already have
255 been evaluated; this method won't evaluate it for you.
258 @param {Function} precompiled Precompiled template function.
259 @param {Object} [options] Options to pass along to the template engine. See
260 template engine docs for options supported by each engine.
261 @return {Function} Compiled template function.
264 revive: function (precompiled, options) {
265 options = options ? Y.merge(this.defaults, options) : this.defaults;
267 return this.engine.revive ? this.engine.revive(precompiled, options) :
272 // Copy existing namespaced properties from Y.Template to the Template function
273 // if Y.Template already exists, then make the function the new Y.Template.
274 // This ensures that other modules can safely add stuff to the Y.Template
275 // namespace even if they're loaded before this one.
276 Y.Template = Y.Template ? Y.mix(Template, Y.Template) : Template;
279 }, '3.13.0', {"requires": ["yui-base"]});