Version 3.16.0
Show:

Template Class

Module: template-base
Parent Module: template

Available since 3.8.0

Provides a generic API for using template engines such as Handlebars and Y.Template.Micro.

Examples

Using with Y.Template.Micro (the default template engine):

YUI().use('template', function (Y) {
    var micro = new Y.Template(),
        html  = micro.render('<%= data.message %>', {message: 'hello!'});

    // ...
});

Using with Handlebars:

YUI().use('template-base', 'handlebars', function (Y) {
    var handlebars = new Y.Template(Y.Handlebars),
        html       = handlebars.render('{{message}}', {message: 'hello!'});

    // ...
});

Constructor

Template

(
  • [engine=Y.Template.Micro]
  • [defaults]
)

Defined in template/js/template-base.js:18

Available since 3.8.0

Parameters:

  • [engine=Y.Template.Micro] Mixed optional

    Template engine to use, such as Y.Template.Micro or Y.Handlebars. Defaults to Y.Template.Micro if not specified.

  • [defaults] Object optional

    Default options to use when instance methods are invoked.

Item Index

Methods

Properties

Methods

compile

(
  • text
  • [options]
)
Function

Defined in template/js/template-base.js:189

Available since 3.8.0

Compiles a template with the current template engine and returns a compiled template function.

Parameters:

  • text String

    Template text to compile.

  • [options] Object optional

    Options to pass along to the template engine. See template engine docs for options supported by each engine.

Returns:

Function: Compiled template function.

get

(
  • templateName
)
Function static

Defined in template/js/template-base.js:130

Available since 3.12.0

Returns the registered template function, given the template name. If an unregistered template is accessed, this will return undefined.

Parameters:

  • templateName String

    The template name.

Returns:

Function: revivedTemplate The revived template function, or undefined if it has not been registered.

precompile

(
  • text
  • [options]
)
String

Defined in template/js/template-base.js:205

Available since 3.8.0

Precompiles a template with the current template engine and returns a string containing JavaScript source code for the precompiled template.

Parameters:

  • text String

    Template text to compile.

  • [options] Object optional

    Options to pass along to the template engine. See template engine docs for options supported by each engine.

Returns:

String: Source code for the precompiled template.

register

(
  • templateName
  • template
)
Function static

Defined in template/js/template-base.js:85

Available since 3.12.0

Registers a pre-compiled template into the central template registry with a given template string, allowing that template to be called and rendered by that name using the Y.Template.render() static method.

For example, given the following simple Handlebars template, in foo.hbs:

Parameters:

  • templateName String

    The template name.

  • template Function

    The function that returns the rendered string. The function should take the following parameters. If a pre-compiled template does not accept these parameters, it is up to the developer to normalize it.

    • [data] Object optional

      Data object to provide when rendering the template.

    • [options] Object optional

      Options to pass along to the template engine. See template engine docs for options supported by each engine.

Returns:

Function: revivedTemplate This is the same function as in template, and is done to maintain compatibility with the Y.Template#revive() method.

Example:

<p>{{tagline}}</p>

It can be precompiled using the Handlebars CLI, and added into a YUI module in the following way. Alternatively, locator can be used to automate this process for you:

YUI.add('templates-foo', function (Y) {

    var engine = new Y.Template(Y.Handlebars),
        precompiled;

    precompiled = // Long precompiled template function here //

    Y.Template.register('foo', engine.revive(precompiled));

}, '0.0.1', {requires: ['template-base', 'handlebars-base']});

See the Y.Template#render method to see how a registered template is used.

render

(
  • templateName
  • [data]
  • [options]
)
String static

Defined in template/js/template-base.js:146

Available since 3.12.0

Renders a template into a string, given the registered template name and data to be interpolated. The template name must have been registered previously with register().

Once the template has been registered and built into a YUI module, it can be listed as a dependency for any other YUI module. Continuing from the above example, the registered template can be used in the following way:

Parameters:

  • templateName String

    The abstracted name to reference the template.

  • [data] Object optional

    The data to be interpolated into the template.

  • [options] Object optional

    Any additional options to be passed into the template.

Returns:

String: output The rendered result.

Example:

YUI.add('bar', function (Y) {

    var html = Y.Template.render('foo', {
        tagline: '"bar" is now template language agnostic'
    });

}, '0.0.1', {requires: ['template-base', 'templates-foo']});

The template can now be used without having to know which specific rendering engine generated it.

render

(
  • text
  • data
  • [options]
)
String

Defined in template/js/template-base.js:221

Available since 3.8.0

Compiles and renders a template with the current template engine in a single step, and returns the rendered result.

Parameters:

  • text String

    Template text to render.

  • data Object

    Data object to provide when rendering the template.

  • [options] Object optional

    Options to pass along to the template engine. See template engine docs for options supported by each engine.

Returns:

String: Rendered result.

revive

(
  • precompiled
  • [options]
)
Function

Defined in template/js/template-base.js:243

Available since 3.8.0

Revives a precompiled template function into an executable template function using the current template engine. The precompiled code must already have been evaluated; this method won't evaluate it for you.

Parameters:

  • precompiled Function

    Precompiled template function.

  • [options] Object optional

    Options to pass along to the template engine. See template engine docs for options supported by each engine.

Returns:

Function: Compiled template function.

Properties

_registry

Object protected static

Defined in template/js/template-base.js:74

Available since 3.12.0

Registry that maps template names to revived template functions.

defaults

Object

Defined in template/js/template-base.js:53

Available since 3.8.1

Default options.

engine

Mixed

Defined in template/js/template-base.js:61

Available since 3.8.0

Template engine class.