---
title: "StringTemplate"
language: "en"
type: "Symbol"
summary: "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."
keywords: 
- templating
- string pattern
- natural language generation
- NLG
- fill in string
- evaluate in string
canonical_url: "https://reference.wolfram.com/language/ref/StringTemplate.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Working with Templates"
    link: "https://reference.wolfram.com/language/guide/WorkingWithTemplates.en.md"
  - 
    title: "String Manipulation"
    link: "https://reference.wolfram.com/language/guide/StringManipulation.en.md"
  - 
    title: "Text Generation"
    link: "https://reference.wolfram.com/language/guide/TextConstruction.en.md"
  - 
    title: "Text Manipulation"
    link: "https://reference.wolfram.com/language/guide/ProcessingTextualData.en.md"
  - 
    title: "Notebook Document Generation"
    link: "https://reference.wolfram.com/language/guide/DocumentGeneration.en.md"
  - 
    title: "External Interpreted Language Interfaces"
    link: "https://reference.wolfram.com/language/guide/ExternalInterpretedLanguageInterfaces.en.md"
  - 
    title: "External Language Interfaces"
    link: "https://reference.wolfram.com/language/guide/ExternalLanguageInterfaces.en.md"
  - 
    title: "LLM-Related Functionality"
    link: "https://reference.wolfram.com/language/guide/LLMFunctions.en.md"
related_workflows: 
  - 
    title: "Create a Webpage with Templates"
    link: "https://reference.wolfram.com/language/workflow/CreateAWebpageWithTemplates.en.md"
related_functions: 
  - 
    title: "FileTemplate"
    link: "https://reference.wolfram.com/language/ref/FileTemplate.en.md"
  - 
    title: "XMLTemplate"
    link: "https://reference.wolfram.com/language/ref/XMLTemplate.en.md"
  - 
    title: "TemplateApply"
    link: "https://reference.wolfram.com/language/ref/TemplateApply.en.md"
  - 
    title: "StringReplace"
    link: "https://reference.wolfram.com/language/ref/StringReplace.en.md"
  - 
    title: "StringJoin"
    link: "https://reference.wolfram.com/language/ref/StringJoin.en.md"
  - 
    title: "StringRiffle"
    link: "https://reference.wolfram.com/language/ref/StringRiffle.en.md"
  - 
    title: "Pluralize"
    link: "https://reference.wolfram.com/language/ref/Pluralize.en.md"
  - 
    title: "StringForm"
    link: "https://reference.wolfram.com/language/ref/StringForm.en.md"
---
# 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 (15)

### Basic Examples (4)

Apply a string template with slots named ``a`` and ``b`` :

```wl
In[1]:= StringTemplate["first `a` then `b`"][<|"a" -> 1234, "b" -> 5678|>]

Out[1]= "first 1234 then 5678"
```

An equivalent form using ``TemplateApply`` :

```wl
In[2]:=
TemplateApply[StringTemplate["first `a` then `b`"], 
	<|"a" -> 1234, "b" -> 5678|>]

Out[2]= "first 1234 then 5678"
```

The ``StringTemplate`` is not needed inside ``TemplateApply`` :

```wl
In[3]:= TemplateApply["first `a` then `b`", <|"a" -> 1234, "b" -> 5678|>]

Out[3]= "first 1234 then 5678"
```

---

Apply a string template to positional arguments:

```wl
In[1]:= StringTemplate["a is ``, b is ``"][5555, 6666]

Out[1]= "a is 5555, b is 6666"
```

An equivalent form:

```wl
In[2]:= StringTemplate["a is `1`, b is `2`"][5555, 6666]

Out[2]= "a is 5555, b is 6666"
```

---

The expression inside `` <  *  ...  *  > `` is evaluated when the string template is applied:

```wl
In[1]:= StringTemplate["Values: <* Range[5] *>."][]

Out[1]= "Values: {1, 2, 3, 4, 5}."
```

Include a template slot as well as an expression in the template string:

```wl
In[2]:= StringTemplate["Value `a`: <* Range[5] *>."][<|"a" -> 1234|>]

Out[2]= "Value 1234: {1, 2, 3, 4, 5}."
```

Values from the association can be used in the template expression:

```wl
In[3]:= StringTemplate["Value `a`: <* Range[#n] *>."][<|"a" -> 1234, "n" -> 3|>]

Out[3]= "Value 1234: {1, 2, 3}."
```

Rule delayed can be used to delay computations:

```wl
In[4]:=
data = <|"a" :> RandomReal[]|>;
TemplateApply["a is `a`", data]

Out[4]= "a is 0.39568856"

In[5]:= TemplateApply["my favorite number is `a`", data]

Out[5]= "my favorite number is 0.14561387"
```

---

Expressions are spliced into the string using ``TextString`` :

```wl
In[1]:= StringTemplate["First `` then ``"]["a string", \[FreeformPrompt]["a million miles"]]

Out[1]= "First a string then 1000000 miles"
```

### Scope (4)

``File`` is fully supported:

```wl
In[1]:= StringTemplate[File["Examples/String/url.txt"]]

Out[1]= TemplateObject[{"http://www.wolframalpha.com/input/?i=", TemplateSlot["input"]}, InsertionFunction -> TextString, CombinerFunction -> StringJoin]
```

---

Templates can be hosted in the Wolfram Cloud:

```wl
In[1]:= template = CloudDeploy[StringTemplate["hello i'm `Name`"]]

Out[1]= CloudObject["https://www.wolframcloud.com/obj/9e2cc434-380e-4b20-9492-66aafdf5b530"]
```

``CloudObject`` can be directly used by ``TemplateApply`` :

```wl
In[2]:= TemplateApply[template, <|"Name" -> "john"|>]

Out[2]= "hello i'm john"
```

---

The second argument of ``StringTemplate`` can be used to bound data to the template:

```wl
In[1]:=
t = StringTemplate["a is `a`, b is `b`", <|"a" :> RandomReal[]|>];
TemplateApply[t]

Out[1]= "a is 0.0193946, b is "

In[2]:= TemplateApply[t, <|"b" :> RandomReal[]|>]

Out[2]= "a is 0.674614, b is 0.908914"
```

---

``URL`` can be used to fetch templates:

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

Out[1]= URL["https://www.wolframcloud.com/obj/26137869-ad07-46f8-9d27-326ae2388d7e"]

In[2]:= URLRead[template, "Body"]

Out[2]= "\"hi i'm `Name`\""

In[3]:= StringTemplate[template]

Out[3]= TemplateObject[{"\"hi i'm ", TemplateSlot["Name"], "\""}, InsertionFunction -> TextString, CombinerFunction -> StringJoin]
```

### Options (2)

#### CombinerFunction (1)

Specify a different way to combine parts of templates:

```wl
In[1]:=
t = StringTemplate[
	"Best friend of `1` is `2`.", CombinerFunction -> Composition[ToUpperCase, StringJoin]
	];
t["John", "Jane"]

Out[1]= "BEST FRIEND OF JOHN IS JANE."
```

It can be used to return any expression, not only a string:

```wl
In[2]:=
t = StringTemplate[
	"Best friend of `1` is `2`.", 
	CombinerFunction -> Row
	];
t["John", "Jane"]

Out[2]= "Best friend of ""John"" is ""Jane""."

In[3]:=
t = StringTemplate[
	"Best friend of `1` is `2`.", 
	CombinerFunction -> Composition[Flatten, Characters]
	];
t["John", "Jane"]

Out[3]= {"B", "e", "s", "t", " ", "f", "r", "i", "e", "n", "d", " ", "o", "f", " ", "J", "o", "h", "n", " ", "i", "s", " ", "J", "a", "n", "e", "."}
```

#### InsertionFunction (1)

Specify a function to convert arguments before they are inserted:

```wl
In[1]:=
t = StringTemplate[
	"Best friend of `1` is `2`.", 
	InsertionFunction -> ToUpperCase
	];
t["John", "Jane"]

Out[1]= "Best friend of JOHN is JANE."
```

You can use a string to specify an export format:

```wl
In[2]:=
t = StringTemplate[
	"Data: `1`", 
	InsertionFunction -> "JSON"
	];
t[<|"name" -> "John", "profession" -> "WebDeveloper"|>]

Out[2]=
"Data: {
    \"name\": \"John\",
    \"profession\": \"WebDeveloper\"
}"
```

### Properties & Relations (3)

``StringTemplate`` evaluates to a ``TemplateObject`` :

```wl
In[1]:= StringTemplate["Best friend of `1` is `2`."]

Out[1]= TemplateObject[{"Best friend of ", TemplateSlot[1], " is ", TemplateSlot[2], "."}, InsertionFunction -> TextString, CombinerFunction -> StringJoin]
```

---

If you deploy a template with data, each time you visit it ``TemplateApply`` will render the content again:

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

Out[1]= CloudObject["https://www.wolframcloud.com/obj/3142181b-e8b5-4506-a061-9a3e4b790066"]
```

---

``TemplateApply`` can be used to parse and apply a string template in one step:

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

Out[1]= "Best friend of John is Jane."
```

### Possible Issues (1)

Data in the second argument of ``StringTemplate`` takes precedence over arguments in ``TemplateApply`` :

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

Out[1]= "a is 1"
```

### Neat Examples (1)

You can deploy a ``StringTemplate`` to the cloud. When you visit the URL, ``TemplateApply`` will render the template on each visit:

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

Out[1]= CloudObject["https://www.wolframcloud.com/obj/63d10ba3-8e0b-432c-8b20-6595a3282556"]
```

## See Also

* [`FileTemplate`](https://reference.wolfram.com/language/ref/FileTemplate.en.md)
* [`XMLTemplate`](https://reference.wolfram.com/language/ref/XMLTemplate.en.md)
* [`TemplateApply`](https://reference.wolfram.com/language/ref/TemplateApply.en.md)
* [`StringReplace`](https://reference.wolfram.com/language/ref/StringReplace.en.md)
* [`StringJoin`](https://reference.wolfram.com/language/ref/StringJoin.en.md)
* [`StringRiffle`](https://reference.wolfram.com/language/ref/StringRiffle.en.md)
* [`Pluralize`](https://reference.wolfram.com/language/ref/Pluralize.en.md)
* [`StringForm`](https://reference.wolfram.com/language/ref/StringForm.en.md)

## Related Guides

* [Working with Templates](https://reference.wolfram.com/language/guide/WorkingWithTemplates.en.md)
* [String Manipulation](https://reference.wolfram.com/language/guide/StringManipulation.en.md)
* [Text Generation](https://reference.wolfram.com/language/guide/TextConstruction.en.md)
* [Text Manipulation](https://reference.wolfram.com/language/guide/ProcessingTextualData.en.md)
* [Notebook Document Generation](https://reference.wolfram.com/language/guide/DocumentGeneration.en.md)
* [External Interpreted Language Interfaces](https://reference.wolfram.com/language/guide/ExternalInterpretedLanguageInterfaces.en.md)
* [External Language Interfaces](https://reference.wolfram.com/language/guide/ExternalLanguageInterfaces.en.md)
* [LLM-Related Functionality](https://reference.wolfram.com/language/guide/LLMFunctions.en.md)

## Related Workflows

* [Create a Webpage with Templates](https://reference.wolfram.com/language/workflow/CreateAWebpageWithTemplates.en.md)

## Related Links

* [An Elementary Introduction to the Wolfram Language: String Patterns and Templates](https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html)

## History

* [Introduced in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md)