#

API Docs for: 3.10.3
Show: Inherited Protected Private Deprecated

# Template Class

Defined in: `template/js/template-base.js:18`
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.

- Index
- Methods
- Properties

## Item Index

### Methods

- compile
- precompile
- render
- revive

### Properties

- defaults
- engine

## Methods

### `compile`

(

- `text`
- `[options]`

)
Function

Defined in `template/js/template-base.js:75`

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:91`

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:107`

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:129`

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

### `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.