TemplateExpression
✖
TemplateExpression
represents an expression held until a template is applied, and then evaluated.
Details and Options

- TemplateExpression has attribute HoldFirst.
- TemplateExpression normally appears inside TemplateObject. It can be generated by StringTemplate, FileTemplate, XMLTemplate, and NotebookTemplate.
- When a template is applied, TemplateExpression[expr] is effectively replaced with the result of evaluating ifunc[expr], where ifunc is value of the option InsertionFunction, which may be specified directly or inherited from other template constructs.
- The default value of the option InsertionFunction is Inherited, which specifies that the value of InsertionFunction should be taken from the enclosing template construct, typically a TemplateObject. If unspecified, Identity will be used, which means the expr will be evaluated and then inserted unchanged.
- In TemplateExpression[expr], the values of any TemplateSlot expressions that occur in expr are inserted before expr is evaluated. These values are not affected by the setting of InsertionFunction.
- In a string template given as a string, TemplateExpression[expr] can be indicated as <*expr*>.
- In a file template, TemplateExpression[expr] can appear in the file as <*expr*>.
- Within <*…*>, any #p… that appear are effectively interpreted as TemplateSlot[p,…].
- In an XML template, TemplateExpression[expr] can appear as <wolfram:expr> expr </wolfram:expr>.
- In a notebook template, a TemplateExpression can typically be inserted using the
button.
Examples
open allclose allBasic Examples (1)Summary of the most common use cases
Options (1)Common values & functionality for each option
InsertionFunction (1)
Use InsertionFunction to specify a function to apply before the TemplateExpression is inserted:

https://wolfram.com/xid/0b0kq0b26aa-du6pg9

The default value of Inherited causes the InsertionFunction to be inherited from any containing template constructs:

https://wolfram.com/xid/0b0kq0b26aa-m43r3b

Specifying an InsertionFunction for the TemplateExpression overrides the value inherited from other constructs:

https://wolfram.com/xid/0b0kq0b26aa-uqjerz

Properties & Relations (1)Properties of the function, and connections to other functions
Functions like StringTemplate generate a TemplateObject that specifies an appropriate InsertionFunction. This InsertionFunction is then inherited by constructs such as TemplateExpression:

https://wolfram.com/xid/0b0kq0b26aa-zfalmb

TemplateExpression[expr] will not evaluate expr before inserting the result into the template, independent of whether evaluation would normally occur in that part of the result:

https://wolfram.com/xid/0b0kq0b26aa-u4g36q

Wolfram Research (2014), TemplateExpression, Wolfram Language function, https://reference.wolfram.com/language/ref/TemplateExpression.html.
Text
Wolfram Research (2014), TemplateExpression, Wolfram Language function, https://reference.wolfram.com/language/ref/TemplateExpression.html.
Wolfram Research (2014), TemplateExpression, Wolfram Language function, https://reference.wolfram.com/language/ref/TemplateExpression.html.
CMS
Wolfram Language. 2014. "TemplateExpression." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/TemplateExpression.html.
Wolfram Language. 2014. "TemplateExpression." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/TemplateExpression.html.
APA
Wolfram Language. (2014). TemplateExpression. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TemplateExpression.html
Wolfram Language. (2014). TemplateExpression. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TemplateExpression.html
BibTeX
@misc{reference.wolfram_2025_templateexpression, author="Wolfram Research", title="{TemplateExpression}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/TemplateExpression.html}", note=[Accessed: 29-March-2025
]}
BibLaTeX
@online{reference.wolfram_2025_templateexpression, organization={Wolfram Research}, title={TemplateExpression}, year={2014}, url={https://reference.wolfram.com/language/ref/TemplateExpression.html}, note=[Accessed: 29-March-2025
]}