Template formatter
Register reusable Smart String templates and apply them by name.
Read time 2 minutesLast updated 5 days ago
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 .
tAnatomy of the template (t) formatter syntax.
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 . Refer to Configure Smart Strings for more information on adding to the formatter.
FormatExceptionThe following example shows how to register templates on the formatter and then add the formatter to a :
SmartFormattervar 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, , or as the format part, . Both are equivalent.
{0:t(fullName)}{0:t:fullName}To reference the current value in scope inside a template, use an empty placeholder , as in the template in the table below.
{}highlightTemplates can be nested: a template can apply another template, for example .
{:t:FIRST} {:t:last}Smart String | Argument | Result |
|---|---|---|
| | The initials are L C |
| | Hello Lara Croft |
| | The name is <color=red>Lara</color>. |
| | The name is <color=red>Croft</color>. |