-
-
Notifications
You must be signed in to change notification settings - Fork 105
Home
axunonb edited this page Feb 7, 2022
·
110 revisions
SmartFormat is a lightweight text templating library written in C#.
It can format various data sources into a string with a minimal, intuitive syntax similar to string.Format.
SmartFormat uses extensions to provide named placeholders, localization, pluralization, gender conjugation, and list and time formatting.
Custom source or formatter extensions can be added easily using simple interfaces.
A very simplistic example:
var data = new { Library = "SmartFormat"};
_ = Smart.Format("Composed with {Library}.", data);
// Result: "Composed with SmartFormat."
Example in string.Format
style:
var stringFormat = string.Format("{0} {0:N2} {1:yyyy-MM-dd}", 5, new DateTime(1900, 12, 31))
var smartFormat = Smart.Format("{0} {0:N2} {1:yyyy-MM-dd}", 5, new DateTime(1900, 12, 31))
// Result: (stringFormat == smartFormat) == true
Example how to format an IList
:
var data = new [] {1, 2, 3, 4, 5};
_ = Smart.Format("{0:list:N2|, |, and }.", data);
// Result: "1.00, 2.00, 3.00, 4.00, and 5.00."
Example for choosing an output string depending on a value:
var data = new[] { new { Name = "John", Gender = 0 },
new { Name = "Mary", Gender = 1 } };
_ = Smart.Format("{Name} commented on {Gender:choose:his|her} photo", data[1]);
// Result: "Mary commented on her photo"
... and there's much more SmartFormat can do.
- 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