-
-
Notifications
You must be signed in to change notification settings - Fork 104
Templates _ TemplateFormatter
axunonb edited this page Mar 30, 2022
·
7 revisions
TemplateFormatter allows you to register reusable templates which contain literal text and placeholders, and include the templates by their name.
{ Any Value : template(option) }
or
{ Any Value : template : option }
Value | formatter name | option |
---|---|---|
Any value | "t" | name of the template to use |
string Name
: default ist
The name to use a named formatter
void Register(string templateName, string template)
: Register a template with its name
public bool Remove(string templateName)
: Remove a template by its name
public void Clear()
: Clear all templates
When adding a template, it will be parsed immediately.
Best practice step by step:
var formatter = Smart.CreateDefaultSmartFormat();
var templates = new TemplateFormatter();
formatter.AddExtensions(templates);
templates.Register("SomeTemplate", "The template content: {variable}");
var templates = new TemplateFormatter();
// Use static default formatter
Smart.Default.AddExtensions(templates);
// ConditionalFormatter with arg1
// controls the kind of salutation
templates.Register("salutation", "{1:cond:{:t:sal_formal}|{:t:sal_informal}}");
// Register the formal nested template
templates.Register("sal_formal", "Dear Mr {LastName}");
// Register the informal nested template
templates.Register("sal_informal", "Hi {Nickname}");
var person = new
{
FirstName = "Joseph",
Nickname = "Joe",
LastName = "Doe"
};
// Use the "master" template with different args
// Informal
Smart.Format("{0:t(salutation)}:", person, false);
// Outputs "Hi Joe:"
// Formal
Smart.Format("{0:t(salutation)}:", person, true);
// Outputs "Dear Mr Doe:"
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License