Template Reference

Table of contents


Introduction

The View uses templates to specify the visible elements. The View accepts Template objects, strings, numbers, or arrays of these.

Template objects capture an XML-like tree of elements. Each Template object has a tag that can be a standard HTML tag string, a published Component string, or a Component class. It also contains any attributes and children associated with each element. Unlike XML, children can be any JavaScipt type.

Template trees are normally built using Template Markup Language (TML).

Constructor

Syntax

new Template(tag, props, children)

Parameters


Instance methods

checkChildTags()

Syntax

checkChildTags(tagsAllowed, msg)

Check that the Template object's children array has valid children.

Parameters

Return value

A boolean. True if the Template has the required children. False otherwise.


clone()

Syntax

clone()

Returns a new Template object identical to this one. Only the direct children of the template are copied.

Return value

A Template object.


findChildrenByClassName()

Syntax

findChildrenByClassName(className, …)

Searches the template tree from this Template object down, for children that have any of the classNames. Returns an array of children that have any of the specified classes.

Parameters

Return value

An array of Template objects representing any descendant of this Template object having any of the specified CSS classNames.


getChildByTag()

Syntax

getChildByTag(tag)

Return the first child that has the specified tag. Only the direct children of the Template object are searched.

Parameters

Return value

Return value A child Template that has a matching tag, or null if not found.


getChildrenByTag()

Syntax

getChildrenByTag(tag)

Return an array of children that have the specified tag. Only the direct children of this Template object are searched.

Return value

An array of children that have any of the specified tags.


splitChildrenByTag()

Syntax

splitChildrenByTag(tag, ...)

Segregate child templates by tag. The arguments are a set of tags. It returns an array containing arrays. The outer array has an array element for each tag argument, plus one more for children not matching any tag argument. Children with matching tags are placed in the results array element corresponding to the tag position in the arguments. Strings, Numbers, and children that don't correspond to any category are placed in the last result array.

Parameters

Return value

An array of arrays.


toString()

Syntax

toString()

Convert this Template and its children into an XML string.

Note: toString can be used on any TML result, including Template objects, strings, or Numbers.

Return value

An XML string.


toText()

Syntax

toText()

Convert this Template's children into a string containing only the text content.

Return value

A string.


Instance properties

children

The children array that was passed to the constructor, then flattened with blank strings filtered out. If no children were passed, then this is an empty array.

Value

An array containing Template objects and/or any JavaScipt type.


props

The props object that was passed to the constructor. If no props object was passed, then this is an empty object.

Value

An object.


tag

The tag string or Component class that was passed to the constructor.

Value

A String or Component class.