Skip to content

v2 TemplateFormatter

axunonb edited this page Feb 6, 2022 · 1 revision

Template Formatter allows you to register reusable templates, and use them by name.

Registering Templates

var smart = Smart.CreateDefaultSmartFormat();
var templates = new TemplateFormatter(smart);
smart.AddExtensions(templates);

templates.Register("firstLast", "{First} {Last}");
templates.Register("lastFirst", "{Last}, {First}");
templates.Register("FIRST", "{First.ToUpper}");
templates.Register("last", "{Last.ToLower}");
templates.Register("NESTED", "{:template(FIRST)} {:template(last)}");

Using Registered Templates

var user = new {First = "First", Last = "Last"};
smart.Format("{:template(firstLast)}", user);
// Uses the "{First} {Last}" template
smart.Format("{:template(NESTED)}", user);
// Uses the "{:template(FIRST)} {:template(last)}" template,
// which will then render "{First.ToUpper} {Last.ToLower}"

Alternative Syntax

The following are equivalent:
{user:template:FIRST}
{user:template(FIRST)}

Clone this wiki locally