TemplateApply[template]
applies a template, evaluating all template elements it contains.
TemplateApply[template,args]
applies a template, using args to fill slots in the template.
TemplateApply
TemplateApply[template]
applies a template, evaluating all template elements it contains.
TemplateApply[template,args]
applies a template, using args to fill slots in the template.
Details and Options
- The template in TemplateApply can be an explicit TemplateObject, as generated from StringTemplate, FileTemplate, XMLTemplate, or NotebookTemplate.
- The following special forms can also be used directly as templates:
-
"string" string template File["filename.xx"] general file template File["filename.html"] etc. XML-like file template File["filename.nb"] etc. notebook-like file template URL[…] a URL pointing to a file template NotebookObject[…] notebook template CloudObject[…] template type based on the contents of the cloud object - In TemplateApply[template,args], args can have the following forms:
-
{arg1,arg2,…} positional arguments <|"name1"->val1,"name2"->val2,…|> named arguments - In string and general file templates, positional arguments corresponding to TemplateSlot[i] can be referred to using `` and `i`. Named arguments corresponding to TemplateSlot["namei"] can be referred to using `namei`. TemplateExpression elements can be given as <*…*>.
Examples
open all close allBasic Examples (3)
TemplateApply[StringTemplate["Hello, my name is ``. I'm feeling ``."], {"Bob", "good"}]TemplateApply[StringTemplate["Hello, my name is `2`. I'm feeling `1`."], {"good", "Bob"}]TemplateApply[StringTemplate["Hello, my name is `name`. I'm feeling `state`."],
<|"name" -> "Bob", "state" -> "good"|>
]TemplateApply works with any expression:
TemplateApply[Mean[{1, 2, 3, TemplateSlot["data"], 5}], <|"data" -> 4|>]TemplateApply can be used directly on strings; in that case, the string is taken to be a StringTemplate:
TemplateApply[
"The orbit of `planet` has period `period`.",
<|"planet" -> "Jupiter", "period" -> Quantity[11.8707, "Years"]|>
]TemplateApply can be used to build held expressions:
res = TemplateApply[Hold[Range[TemplateSlot["range", InsertionFunction -> Factorial]]], <|"range" :> 1 + 3|>]ReleaseHold[res]Scope (5)
TemplateApply can be used to parse and apply a StringTemplate in one step:
TemplateApply[
"Best friend of `1` is `2`.",
{"John", "Jane"}
]File is fully supported:
TemplateApply[File["Examples/String/url.txt"], <|"input" -> URLEncode@"200 usd in eur"|>]Templates can be hosted in the Wolfram Cloud:
template = CloudDeploy[StringTemplate["hello i'm `Name`"]]CloudObject can be directly used by TemplateApply:
TemplateApply[template, <|"Name" -> "john"|>]The second argument of any TemplateObject can be used to bound data to the template:
t = StringTemplate["a is `a`, b is `b`", <|"a" :> RandomReal[]|>];
TemplateApply[t]TemplateApply[t, <|"b" :> RandomReal[]|>]URL can be used to fetch templates:
template = URL@@CloudExport["hi i'm `Name`", "String"]URLRead[template, "Body"]StringTemplate[template]URL can be directly used by TemplateApply:
TemplateApply[template, <|"Name" -> "john"|>]Generalizations & Extensions (2)
CombinerFunction (1)
Specify a different way to combine parts of templates:
Using CombinerFunction:
TemplateApply[StringTemplate["My name is `name`. I'm feeling `state`.", CombinerFunction -> Composition[ToUpperCase, StringJoin]], <|"name" -> "Bob", "state" -> "good"|>]Applications (1)
TemplateApply can be used to build complex expressions:
test = TemplateApply[
Hold[
VerificationTest[
TemplateSlot["test"],
TemplateEvaluate[TemplateSlot["test"]]
]
],
<|"test" :> 4 + 4|>
]ReleaseHold[test]Properties & Relations (1)
If you deploy a template with data, each time you visit it TemplateApply will render the content again:
CloudDeploy[
StringTemplate[
"hello `Name`",
<|"Name" :> If[$RequesterWolframID === None, "friend", $RequesterWolframID]|>
]]Neat Examples (1)
You can deploy any TemplateObject to the cloud. When you visit the URL, TemplateApply will render the template on each visit:
CloudDeploy[
StringTemplate[
"now is: `Now`",
<|"Now" :> Now|>
]]Related Workflows
- Generate a Report According to a Schedule ▪
- Create a Webpage with Templates
Text
Wolfram Research (2014), TemplateApply, Wolfram Language function, https://reference.wolfram.com/language/ref/TemplateApply.html (updated 2016).
CMS
Wolfram Language. 2014. "TemplateApply." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/TemplateApply.html.
APA
Wolfram Language. (2014). TemplateApply. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TemplateApply.html
BibTeX
@misc{reference.wolfram_2026_templateapply, author="Wolfram Research", title="{TemplateApply}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/TemplateApply.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_templateapply, organization={Wolfram Research}, title={TemplateApply}, year={2016}, url={https://reference.wolfram.com/language/ref/TemplateApply.html}, note=[Accessed: 13-June-2026]}