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 allclose allBasic Examples (4)
Apply a string template with slots named a and b:
An equivalent form using TemplateApply:
The StringTemplate is not needed inside TemplateApply:
Apply a string template to positional arguments:
The expression inside <* ... *> is evaluated when the string template is applied:
Include a template slot as well as an expression in the template string:
Values from the association can be used in the template expression:
Rule delayed can be used to delay computations:
Expressions are spliced into the string using TextString:
Scope (4)
File is fully supported:
Templates can be hosted in the Wolfram Cloud:
CloudObject can be directly used by TemplateApply:
The second argument of StringTemplate can be used to bound data to the template:
URL can be used to fetch templates:
Options (2)
Properties & Relations (3)
StringTemplate evaluates to a TemplateObject:
If you deploy a template with data, each time you visit it TemplateApply will render the content again:
TemplateApply can be used to parse and apply a string template in one step:
Possible Issues (1)
Data in the second argument of StringTemplate takes precedence over arguments in TemplateApply:
Neat Examples (1)
You can deploy a StringTemplate to the cloud. When you visit the URL, TemplateApply will render the template on each visit:
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