-
-
Notifications
You must be signed in to change notification settings - Fork 107
Creating Your Own Extension
axunonb edited this page Mar 11, 2022
·
1 revision
public class HelloFormatter : IFormatter
{
public string Name { get; set; } = "hello";
public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
var iCanHandleThisInput = formattingInfo.CurrentValue is bool;
if (!iCanHandleThisInput)
return false;
formattingInfo.Write("HELLO ");
if ((bool) formattingInfo.CurrentValue)
formattingInfo.Write(formattingInfo.FormatterOptions);
else
formattingInfo.Write(formattingInfo.Format.GetLiteralText());
return true;
}
}
Smart.Default.AddExtensions(new HelloFormatter());
Smart.Format("{value:hello(world):earth}", new { value = true });
// Outputs: "HELLO world"
Smart.Format("{value:hello(world):earth}", new { value = false });
// Outputs: "HELLO earth"
- 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