({{, (RefLink[TemplateExpression, paclet:ref/TemplateExpression])[expr] 
represents an expression held until a template is applied, and then evaluated.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[TemplateExpression, paclet:ref/TemplateExpression]) has attribute (RefLink[HoldFirst, paclet:ref/HoldFirst]).

(RefLink[TemplateExpression, paclet:ref/TemplateExpression]) normally appears inside (RefLink[TemplateObject, paclet:ref/TemplateObject]). It can be generated by (RefLink[StringTemplate, paclet:ref/StringTemplate]), (RefLink[FileTemplate, paclet:ref/FileTemplate]), (RefLink[XMLTemplate, paclet:ref/XMLTemplate]), and (RefLink[NotebookTemplate, paclet:ref/NotebookTemplate]).

When a template is applied, (RefLink[TemplateExpression, paclet:ref/TemplateExpression])[expr] is effectively replaced with the result of evaluating ifunc[expr], where ifunc is value of the option (RefLink[InsertionFunction, paclet:ref/InsertionFunction]), which may be specified directly or inherited from other template constructs.

The default value of the option (RefLink[InsertionFunction, paclet:ref/InsertionFunction]) is (RefLink[Inherited, paclet:ref/Inherited]), which specifies that the value of (RefLink[InsertionFunction, paclet:ref/InsertionFunction]) should be taken from the enclosing template construct, typically a (RefLink[TemplateObject, paclet:ref/TemplateObject]). If unspecified, (RefLink[Identity, paclet:ref/Identity]) will be used, which means the expr will be evaluated and then inserted unchanged.

In (RefLink[TemplateExpression, paclet:ref/TemplateExpression])[expr], the values of any (RefLink[TemplateSlot, paclet:ref/TemplateSlot]) expressions that occur in expr are inserted before expr is evaluated. These values are not affected by the setting of (RefLink[InsertionFunction, paclet:ref/InsertionFunction]).

In a string template given as a string, (RefLink[TemplateExpression, paclet:ref/TemplateExpression])[expr] can be indicated as <*expr*>.

In a file template, (RefLink[TemplateExpression, paclet:ref/TemplateExpression])[expr] can appear in the file as <*expr*>.

Within <*…*>, any #p… that appear are effectively interpreted as (RefLink[TemplateSlot, paclet:ref/TemplateSlot])[p,…].

In an XML template, (RefLink[TemplateExpression, paclet:ref/TemplateExpression])[expr] can appear as <wolfram:expr> expr </wolfram:expr>.

In a notebook template, a (RefLink[TemplateExpression, paclet:ref/TemplateExpression]) can typically be inserted using the  button.

OpenersPrimaryExamplesSection(Spacer1)Examples  (3)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Prevent evaluation until a template is applied:

t = TemplateExpression[RandomInteger[100]]

TemplateApply[t]

TemplateApply[t]

OpenersExampleSection1(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)InsertionFunction  (1)

Use (RefLink[InsertionFunction, paclet:ref/InsertionFunction]) to specify a function to apply before the (RefLink[TemplateExpression, paclet:ref/TemplateExpression]) is inserted:

t="Today is "~~ TemplateExpression[Today,InsertionFunction -> DateString];
TemplateApply[t]

The default value of (RefLink[Inherited, paclet:ref/Inherited]) causes the (RefLink[InsertionFunction, paclet:ref/InsertionFunction]) to be inherited from any containing template constructs:

t="Today is "~~ TemplateExpression[Today];
TemplateApply[TemplateObject[t,InsertionFunction -> DateString]]

Specifying an (RefLink[InsertionFunction, paclet:ref/InsertionFunction]) for the (RefLink[TemplateExpression, paclet:ref/TemplateExpression]) overrides the value inherited from other constructs:

t="Today is "~~ TemplateExpression[Today,InsertionFunction -> (DateString[#,"DayName"]&)];
TemplateApply[TemplateObject[t,InsertionFunction -> DateString]]

OpenersExampleSection2(Spacer1)Properties & Relations  (1)

Functions like (RefLink[StringTemplate, paclet:ref/StringTemplate]) generate a (RefLink[TemplateObject, paclet:ref/TemplateObject]) that specifies an appropriate (RefLink[InsertionFunction, paclet:ref/InsertionFunction]). This (RefLink[InsertionFunction, paclet:ref/InsertionFunction]) is then inherited by constructs such as (RefLink[TemplateExpression, paclet:ref/TemplateExpression]):

StringTemplate["the time is <*TimeObject[]*>"]

(RefLink[TemplateExpression, paclet:ref/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:

TemplateApply[
Hold[TemplateExpression[2+2]]
]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[TemplateSlot, paclet:ref/TemplateSlot])  ▪  (RefLink[TemplateObject, paclet:ref/TemplateObject])  ▪  (RefLink[TemplateApply, paclet:ref/TemplateApply])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Working with Templates, paclet:guide/WorkingWithTemplates])}, {▪(RefLinkPlain[String Manipulation, paclet:guide/StringManipulation])}, {▪(RefLinkPlain[XML Templates, paclet:guide/XMLTemplates])}})}})