TemplateIf[condition,tclause]
represents an element of a template object that inserts tclause if the condition evaluates to True.
TemplateIf[condition,tclause,fclause]
inserts fclause if the condition does not evaluate to True.
TemplateIf
TemplateIf[condition,tclause]
represents an element of a template object that inserts tclause if the condition evaluates to True.
TemplateIf[condition,tclause,fclause]
inserts fclause if the condition does not evaluate to True.
Details and Options
- condition can be either a TemplateSlot or TemplateExpression.
- The tclause is used when condition===True.
- If an fclause is not provided, the value Sequence[] is used instead.
- In an XML template, TemplateIf can be indicated using <wolfram:condition>...</wolfram:condition>. Multiple conditions can be nested using <wolfram:switch><wolfram:condition>...</wolfram:condition>...<wolfram:else>...</wolfram:else></wolfram:switch>.
- TemplateIf has the option InsertionFunction, with default setting Inherited, which specifies how the content of each template slot should be formatted before inserting it.
Examples
open all close allBasic Examples (1)
Create a TemplateIf expression:
template = TemplateIf[
TemplateSlot["test"],
"Ok",
Style["Not ok", Red]
]Evaluate the expression by passing some data to it:
TemplateApply[template, <|"test" -> RandomInteger[] === 0|>]Scope (2)
The TemplateIf expression can contain a test that does not evaluate immediately:
template = TemplateIf[
StringQ[TemplateSlot[1]],
TemplateSlot[1],
"please insert a string"
]Evaluate the expression by passing some data to it:
TemplateApply[template, 23]TemplateApply[template, "hello"]Create a TemplateIf expression with one argument:
template = TemplateIf[
NumberQ[TemplateSlot[1]],
TemplateSlot[1]
]If the condition evaluates to true, it returns the second argument of TemplateIf:
TemplateApply[Row[{"Numbers: ", template}], 12]If the condition evaluates to false, it returns a Sequence, useful to eliminate the expression entirely:
TemplateApply[Row[{"Numbers: ", template}], None]Options (1)
InsertionFunction (1)
Using an InsertionFunction:
t = TemplateIf[
MatchQ[TemplateSlot["planets"], _List],
TemplateSlot["planets"],
"Please insert data",
InsertionFunction -> "json"
];
TemplateApply[t, <|"planets" -> {"Earth", "Mars", "Jupiter"}|>]InsertionFunction can be any function:
t = TemplateIf[
MatchQ[TemplateSlot["planet"], _String],
TemplateSlot["planet"],
"Please insert a planet",
InsertionFunction -> ToUpperCase
];
TemplateApply[t, <|"planet" -> "Jupiter"|>]Possible Issues (1)
The first argument of TemplateIf does not evaluate immediately:
template = TemplateIf[TemplateSlot[1] === "admin", "You can view the content", "You cannot view the content"]Evaluation is done when the template is applied.
TemplateApply[template, "admin"]See Also
Related Guides
History
Text
Wolfram Research (2014), TemplateIf, Wolfram Language function, https://reference.wolfram.com/language/ref/TemplateIf.html.
CMS
Wolfram Language. 2014. "TemplateIf." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/TemplateIf.html.
APA
Wolfram Language. (2014). TemplateIf. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TemplateIf.html
BibTeX
@misc{reference.wolfram_2026_templateif, author="Wolfram Research", title="{TemplateIf}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/TemplateIf.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_templateif, organization={Wolfram Research}, title={TemplateIf}, year={2014}, url={https://reference.wolfram.com/language/ref/TemplateIf.html}, note=[Accessed: 13-June-2026]}