StringTemplate["string"]
yields a TemplateObject expression that represents a string template to be applied to arguments.
StringTemplate[src]
uses File[…], URL[…] or CloudObject[…] as the source for the string template.
StringTemplate[form,args]
yields a TemplateObject with arguments, suitable for cloud deployment or other evaluation.
StringTemplate
StringTemplate["string"]
yields a TemplateObject expression that represents a string template to be applied to arguments.
StringTemplate[src]
uses File[…], URL[…] or CloudObject[…] as the source for the string template.
StringTemplate[form,args]
yields a TemplateObject with arguments, suitable for cloud deployment or other evaluation.
Details and Options
- The following special forms can be used inside the string:
-
`` TemplateSlot[n], for n taking successive values 1, 2, 3, … `n` TemplateSlot[n] `name` TemplateSlot["name"] <*expr*> TemplateExpression[expr] - A StringTemplate object can be rendered using TemplateApply[StringTemplate[…],{e1,e2,…}] or using the form StringTemplate[…][e1,e2,…].
- StringTemplate has the following options:
-
CombinerFunction StringJoin function to apply to combine pieces before returning a result InsertionFunction TextString function or format to apply before inserting expressions - In StringTemplate[File[…]] and StringTemplate[URL[…]], the content of the source file etc. is imported as a string.
Examples
open all close allBasic Examples (4)
Apply a string template with slots named a and b:
StringTemplate["first `a` then `b`"][<|"a" -> 1234, "b" -> 5678|>]An equivalent form using TemplateApply:
TemplateApply[StringTemplate["first `a` then `b`"],
<|"a" -> 1234, "b" -> 5678|>]The StringTemplate is not needed inside TemplateApply:
TemplateApply["first `a` then `b`", <|"a" -> 1234, "b" -> 5678|>]Apply a string template to positional arguments:
StringTemplate["a is ``, b is ``"][5555, 6666]StringTemplate["a is `1`, b is `2`"][5555, 6666]The expression inside <* ... *> is evaluated when the string template is applied:
StringTemplate["Values: <* Range[5] *>."][]Include a template slot as well as an expression in the template string:
StringTemplate["Value `a`: <* Range[5] *>."][<|"a" -> 1234|>]Values from the association can be used in the template expression:
StringTemplate["Value `a`: <* Range[#n] *>."][<|"a" -> 1234, "n" -> 3|>]Rule delayed can be used to delay computations:
data = <|"a" :> RandomReal[]|>;
TemplateApply["a is `a`", data]TemplateApply["my favorite number is `a`", data]Expressions are spliced into the string using TextString:
StringTemplate["First `` then ``"]["a string", ["a million miles"]]Scope (4)
File is fully supported:
StringTemplate[File["Examples/String/url.txt"]]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 StringTemplate 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]Options (2)
CombinerFunction (1)
Specify a different way to combine parts of templates:
t = StringTemplate[
"Best friend of `1` is `2`.", CombinerFunction -> Composition[ToUpperCase, StringJoin]
];
t["John", "Jane"]It can be used to return any expression, not only a string:
t = StringTemplate[
"Best friend of `1` is `2`.",
CombinerFunction -> Row
];
t["John", "Jane"]t = StringTemplate[
"Best friend of `1` is `2`.",
CombinerFunction -> Composition[Flatten, Characters]
];
t["John", "Jane"]InsertionFunction (1)
Specify a function to convert arguments before they are inserted:
t = StringTemplate[
"Best friend of `1` is `2`.",
InsertionFunction -> ToUpperCase
];
t["John", "Jane"]You can use a string to specify an export format:
t = StringTemplate[
"Data: `1`",
InsertionFunction -> "JSON"
];
t[<|"name" -> "John", "profession" -> "WebDeveloper"|>]Properties & Relations (3)
StringTemplate evaluates to a TemplateObject:
StringTemplate["Best friend of `1` is `2`."]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]|>
]]TemplateApply can be used to parse and apply a string template in one step:
TemplateApply[
"Best friend of `1` is `2`.",
{"John", "Jane"}
]Possible Issues (1)
Data in the second argument of StringTemplate takes precedence over arguments in TemplateApply:
t = StringTemplate["a is `a`", <|"a" -> 1|>];
TemplateApply[t, <|"a" -> 2|>]Neat Examples (1)
You can deploy a StringTemplate to the cloud. When you visit the URL, TemplateApply will render the template on each visit:
t = StringTemplate[
"Hello <strong>world</strong> now is `now`",
<|"now" :> Now|>
];
CloudDeploy[t]Related Workflows
- Create a Webpage with Templates
History
Text
Wolfram Research (2014), StringTemplate, Wolfram Language function, https://reference.wolfram.com/language/ref/StringTemplate.html.
CMS
Wolfram Language. 2014. "StringTemplate." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/StringTemplate.html.
APA
Wolfram Language. (2014). StringTemplate. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringTemplate.html
BibTeX
@misc{reference.wolfram_2026_stringtemplate, author="Wolfram Research", title="{StringTemplate}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/StringTemplate.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_stringtemplate, organization={Wolfram Research}, title={StringTemplate}, year={2014}, url={https://reference.wolfram.com/language/ref/StringTemplate.html}, note=[Accessed: 12-June-2026]}