SimpleTemplate (class)

Categories:Text

Overview

Summary

The SimpleTemplate class is designed to very simply build text output in an object-oriented manner, by combining the contents of template string with a hash of values. Named properties of the hash map directly to named {placeholder} variables within the input template, which is populated to produce new output.

By separating content (template) and data (object properties) it makes for cleaner and more easily-maintainable code, rather than lots of multiline strings, escaping, and concatenation.

Also see the full Template class which has some more heavyweight features for advanced templating, such as loading from a file, nested templating, and more advanced text handling.

Contents

Concept

Template

The SimpleTemplate class combines an input template string with a data object to create new output based on a combination of the two.

A typical template string might look something like the following:

'Hello there {name}, your user id is {id}'

The words in curly braces, such as {name}, are placeholder variables, which are meant to be populated by the values of properties contained in a data object supplied by the developer.

Note that you can use nested property names if it makes sense, for example reporting the properties of a Library Item:

'The item has {timeline.frameCount} frames'
Data object

The data object can be any object with properties that can be mapped to the template's placeholder variables, usually an object literal of your creation:

var data =
{
    name: 'Dave',
    id: 123
}
Output

To combine the two elements, you simply create a SimpleTemplate instance, and trace its output:

var input      = 'Hello there {name}, your user id is {id}';
var data       = { name:'Dave', id:123 };

var template   = new SimpleTemplate(input, data);

trace(template.output);
Hello there Dave, your user id is 123

Note that you may pass any object as a data object in this case a Symbol instance:

var input      = 'The element "{name}" is {width}px wide';
var data       = $selection[0];

new SimpleTemplate(input, data).render();
The element "clip1" is 70px wide

API

SimpleTemplate(input, data)

SimpleTemplate constructor

Parameters:

  • param String template The input template, including {placeholder} variables
  • param Object data An optional Object of name:value pairs

The following example combines a basic object literal with a template, and assigns it to a variable:

var template = new SimpleTemplate('Hello there {name}', {name:'Dave'});
var result = template.output;
Hello there Dave

If you wish to trace the result directly to the Output panel, you can also use render() method.

populate(data)

Populates the template with data

Parameters:

  • param Object data An Object of name:value pairs

Returns:

  •   SimpleTemplate The current instance

The following example populates then repopulates a single template with different sets of data (and renders both to the output panel):

var template = new SimpleTemplate('Hello there {name}');
template
	.populate({name:'Dave'})
	.render()
	.populate({name:'John'})
	.render();
Hello there Dave
Hello there John

Note that you can also populate a template with any object, for example, a Library Item, and even reference deep properties using dot-syntax:

var collection  = $$(':selected');
var template    = new SimpleTemplate('The item "{name}" has {timeline.frameCount} frames');

for each(var item in collection.elements)
{
	template.populate(item).render();
}
The item "Symbol 5" has 13 frames
The item "Symbol 4" has 37 frames
The item "Symbol 3" has 11 frames
The item "Symbol 2" has 33 frames
The item "Symbol 1" has 4 frames


update(input)

Updates the input template with a new value (and automatically repopulates output)

Parameters:

  • param String input A String input template

Returns:

  •   SimpleTemplate The current instance

The following example renders two sets of input templates using the same original data:

var template = new SimpleTemplate('Hello there {name}', {name:'Dave'});
template
.render()
.update('Goodbye now {name}')
.render();
Hello there Dave
Goodbye now Dave

render(output)

Prints the populated output of the input

Parameters:

  • param Boolean output An optional flag to print the table table to the Output panel, defaults to true

Returns:

  •   String The populated output

The render() method is provided purely for the convenience of chaining method calls (note that you can also access populated output directly via the output property).

The following example demonstrates both rendering to the output panel, and to a variable:

var template = new SimpleTemplate('Hello there {name}', {name:'Dave'});

// trace to the output panel
template.render();

// assign to a variable
var result = template.render(false);
// Hello there Dave

output

The result of the populated template

  • Type: String
  • Access: Read

The following example assigns the result of a template population to an external variable:

var template = new SimpleTemplate('Hello there {name}', {name:'Dave'});
var result = template.output;
Hello there Dave

toString()

Returns the String representation of the SimpleTemplate

The following example prints a summary of the SimlpeTemplate instance to the Output panel:

trace(template);		
[object SimpleTemplate input="Hello there {name}"]

Comments are closed.