Working with Templates

The Wolfram Language has a powerful symbolic templating framework that can be used with strings, files, XML-like structures, notebooks, and other constructs.

Types of Templates

StringTemplate  ▪  FileTemplate  ▪  XMLTemplate  ▪  NotebookTemplate

TemplateObject general symbolic template

Symbolic Template Elements

TemplateSlot a slot to fill when the template is applied

TemplateExpression an arbitrary expression evaluated when the template is applied

TemplateWith a way to define local variables inside templates

TemplateIf a conditional to use when the template is applied

TemplateSequence specification of a sequence to assemble during template application

Text Transformation

Pluralize pluralize a word if appropriate

Capitalize  ▪  TextString  ▪  StringRepeat  ▪  ...

Template Syntax in Strings and Text Files

<* expr *> general template expression (TemplateExpression)

`` successive template slots (TemplateSlot[1], )

`i` numbered template slots

`name` named template slots (TemplateSlot["name"])

#, #i, #name template slots appearing inside <* *>

Template Tags for XML-Like Documents

<wolfram:expr></wolfram:expr> tags for a template expression

<wolfram:slot> tag for a template slot

<wolfram:sequence> tag for a template sequence

<wolfram:if>  ▪  <wolfram:with>  ▪  <wolfram:get>  ▪  ...

Template Notebooks

File New Template Notebook open a new template notebook

CreateNotebook programmatically open a blank template notebook

GenerateDocument generate a report by applying a template notebook to data

button to insert a template slot in a notebook

button to insert a template expression in a notebook

Applying Templates

TemplateApply apply a template, returning the expression generated

FileTemplateApply apply a template, putting the result in a file

GenerateDocument generate a report using a template