API Docs for: 3.8.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]
)

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.

Item Index

Properties

Methods

compile

(
  • text
  • [options]
)
Function

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

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.

precompile

(
  • text
  • [options]
)
String

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

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.

render

(
  • text
  • data
  • [options]
)
String

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

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:115

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

engine

Mixed

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

Available since 3.8.0

Template engine class.