({{, (RefLink[StringTemplate, paclet:ref/StringTemplate])["string"] 
yields a (RefLink[TemplateObject, paclet:ref/TemplateObject]) expression that represents a string template to be applied to arguments. }, {, (RefLink[StringTemplate, paclet:ref/StringTemplate])[src]
uses (RefLink[File, paclet:ref/File])[…], (RefLink[URL, paclet:ref/URL])[…] or (RefLink[CloudObject, paclet:ref/CloudObject])[…] as the source for the string template.}, {, (RefLink[StringTemplate, paclet:ref/StringTemplate])[form,args]
yields a (RefLink[TemplateObject, paclet:ref/TemplateObject]) with arguments, suitable for cloud deployment or other evaluation.}})

OpenersNotesSection(Spacer1)Details and Options

The following special forms can be used inside the string:

A (RefLink[StringTemplate, paclet:ref/StringTemplate]) object can be rendered using (RefLink[TemplateApply, paclet:ref/TemplateApply])[StringTemplate[…],{(Subscript[e, 1]),(Subscript[e, 2]),…}] or using the form (RefLink[StringTemplate, paclet:ref/StringTemplate])[…][(Subscript[e, 1]),(Subscript[e, 2]),…].

(RefLink[StringTemplate, paclet:ref/StringTemplate]) has the following options:

In (RefLink[StringTemplate, paclet:ref/StringTemplate])[(RefLink[File, paclet:ref/File])[…]] and (RefLink[StringTemplate, paclet:ref/StringTemplate])[(RefLink[URL, paclet:ref/URL])[…]], the content of the source file etc. is imported as a string.

OpenersPrimaryExamplesSection(Spacer1)Examples  (15)

OpenersExampleSection0(Spacer1)Basic 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 (RefLink[TemplateApply, paclet:ref/TemplateApply]):

TemplateApply[StringTemplate["first `a` then `b`"],
<|"a"->1234,"b"->5678|>]

The (RefLink[StringTemplate, paclet:ref/StringTemplate]) is not needed inside (RefLink[TemplateApply, paclet:ref/TemplateApply]):

TemplateApply["first `a` then `b`",<|"a"->1234,"b"->5678|>]

Apply a string template to positional arguments:

StringTemplate["a is ``, b is ``"][5555,6666]

An equivalent form:

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

StringTemplate["First `` then ``"]["a string",LinguisticAssistant]

OpenersExampleSection1(Spacer1)Scope  (4)

(RefLink[File, paclet:ref/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`"]]

(RefLink[CloudObject, paclet:ref/CloudObject]) can be directly used by (RefLink[TemplateApply, paclet:ref/TemplateApply]):

TemplateApply[template, <|"Name" -> "john"|>]

The second argument of (RefLink[StringTemplate, paclet:ref/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[]|>]

(RefLink[URL, paclet:ref/URL]) can be used to fetch templates:

template = URL @@ CloudExport["hi i'm `Name`", "String"]

URLRead[template, "Body"]

StringTemplate[template]

OpenersExampleSection2(Spacer1)Options  (2)

OpenersExampleSubsection0(Spacer1)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"]

OpenersExampleSubsection1(Spacer1)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"|>]

OpenersExampleSection3(Spacer1)Properties & Relations  (3)

(RefLink[StringTemplate, paclet:ref/StringTemplate]) evaluates to a (RefLink[TemplateObject, paclet:ref/TemplateObject]):

StringTemplate["Best friend of `1` is `2`."]

If you deploy a template with data, each time you visit it (RefLink[TemplateApply, paclet:ref/TemplateApply]) will render the content again:

CloudDeploy[
StringTemplate[
"hello `Name`", 
<|"Name" :> If[$RequesterWolframID === None, "friend", $RequesterWolframID]|>
]]

(RefLink[TemplateApply, paclet:ref/TemplateApply]) can be used to parse and apply a string template in one step:

TemplateApply[
"Best friend of `1` is `2`.",
{"John","Jane"}
]

OpenersExampleSection4(Spacer1)Possible Issues  (1)

Data in the second argument of (RefLink[StringTemplate, paclet:ref/StringTemplate]) takes precedence over arguments in (RefLink[TemplateApply, paclet:ref/TemplateApply]):

t = StringTemplate["a is `a`", <|"a" ->  1|>];
TemplateApply[t, <|"a" ->  2|>]

OpenersExampleSection5(Spacer1)Neat Examples  (1)

You can deploy a (RefLink[StringTemplate, paclet:ref/StringTemplate]) to the cloud. When you visit the URL, (RefLink[TemplateApply, paclet:ref/TemplateApply]) will render the template on each visit:

t = StringTemplate[
"Hello <strong>world</strong> now is `now`", 
<|"now" :> Now|>
];
CloudDeploy[t]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[FileTemplate, paclet:ref/FileTemplate])  ▪  (RefLink[XMLTemplate, paclet:ref/XMLTemplate])  ▪  (RefLink[TemplateApply, paclet:ref/TemplateApply])  ▪  (RefLink[StringReplace, paclet:ref/StringReplace])  ▪  (RefLink[StringJoin, paclet:ref/StringJoin])  ▪  (RefLink[StringRiffle, paclet:ref/StringRiffle])  ▪  (RefLink[Pluralize, paclet:ref/Pluralize])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[String Manipulation, paclet:guide/StringManipulation])}, {▪(RefLinkPlain[Text Generation, paclet:guide/TextConstruction])}, {▪(RefLinkPlain[Working with Templates, paclet:guide/WorkingWithTemplates])}, {▪(RefLinkPlain[Text Manipulation, paclet:guide/ProcessingTextualData])}, {▪(RefLinkPlain[Notebook Document Generation, paclet:guide/DocumentGeneration])}, {▪(RefLinkPlain[External Interpreted Language Interfaces, paclet:guide/ExternalInterpretedLanguageInterfaces])}, {▪(RefLinkPlain[External Language Interfaces, paclet:guide/ExternalLanguageInterfaces])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])(WebLink[: String Patterns and Templates , https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])}})}})