Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


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
t
.

Anatomy 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
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>.

Additional resources