Register reusable Smart String templates and apply them by name.
The Template formatter stores named templates so you can reuse a piece of formatting across many Smart Strings. When the formatting needs to change, you edit the template in one place. Apply it explicitly with the name t.
flowchart TD
full["{value:t(templateName)}"]
full --> p1["value"]
full --> p2["t"]
full --> p3["templateName"]
p1 --> d1["Any value"]
p2 --> d2["Formatter name: t"]
p3 --> d3["Name of a registered<br/>template"]
classDef code fill:#F5F5F5,stroke:#BDBDBD,color:#111;
classDef orange fill:none,stroke:none,color:#F37021;
classDef green fill:none,stroke:none,color:#67BC6B;
classDef pink fill:none,stroke:none,color:#EB417A;
class full code;
class p1,d1 orange;
class p2,d2 green;
class p3,d3 pink;
linkStyle default stroke:#2196F3,stroke-width:1.5px;
Template names are matched using the formatter’s case sensitivity setting. By default, matching is case-insensitive.
Register the templates you create and add them to the formatter so they can be used. The Template formatter can’t auto-detect, so you must always apply it by name. Applying an unregistered template name throws a FormatException. Refer to Configure Smart Strings for more information on adding to the formatter.
The following example shows how to register templates on the formatter and then add the formatter to a SmartFormatter:
var templates = new TemplateFormatter();
templates.Register("fullName", "{name} {surname}");
templates.Register("initials", "{name:substr(0,1)} {surname:substr(0,1)}");
templates.Register("highlight", "<color=red>{}</color>");
Smart.Default.AddExtensions(templates);
Pass the template name as a formatter option, {0:t(fullName)}, or as the format part, {0:t:fullName}. Both are equivalent.
To reference the current value in scope inside a template, use an empty placeholder {}, as in the highlight template in the table below.
Templates can be nested: a template can apply another template, for example {:t:FIRST} {:t:last}.
| Smart String | Argument | Result |
|---|---|---|
The initials are {0:t(initials)} |
name = "Lara", surname = "Croft"
|
The initials are L C |
Hello {0:t(fullName)} |
name = "Lara", surname = "Croft"
|
Hello Lara Croft |
The name is {name:t(highlight)}. |
name = "Lara" |
The name is <color=red>Lara</color>. |
The name is {surname:t(highlight)}. |
surname = "Croft" |
The name is <color=red>Croft</color>. |