-
-
Notifications
You must be signed in to change notification settings - Fork 105
Home
Norbert Bietsch edited this page Dec 3, 2016
·
110 revisions
SmartFormat is a lightweight templating library, with an emphasis on grammar.
It makes it easy for a data-driven template to have proper pluralization, gender conjugation, lists, and conditional language logic. Named placeholder give a more intuitive and less error-prone way to introduce variables.
Note: Before getting started, carefully read the chapter about Common Pitfalls.
var emails = new List<string>() {"email1", "email2", "email3"};
Smart.Format("You have {0} new {0:message|messages}", emails.Count);
// Outputs: "You have 3 messages"
Exmample with named placeholders and indexed parameter:
var user = new[] { new { Name = "John", Gender = 0 }, new { Name = "Mary", Gender = 1 } };
Smart.Format("{Name} commented on {Gender:his|her|their} photo", user[1]);
// Outputs: "Mary commented on her photo"
The following code with indexed placeholders leads to the same result as above:
var user = new[] { new { Name = "John", Gender = 0 }, new { Name = "Mary", Gender = 1 } };
Smart.Default.Parser.UseAlternativeEscapeChar('\\');
Smart.Format("{1:{Name}} commented on {1:{Gender:his|her|their}} photo", user);
var Users = new[] { new { Name = "Jim" }, new { Name = "Pam" }, new { Name = "Dwight" }, new { Name = "Michael" } };
Smart.Format("{Users:{Name}|, | and } liked your comment", new object[] { new {Users = Users}});
// Outputs: "Jim, Pam, Dwight and Michael liked your comment"
Choose (conditional logic)
var emails = new List<string>() {"email1", "email2", "email3"};
Smart.Format("You have {Messages.Count:choose(0|1):no new messages|a new message|{} new messages}", new object[] {new {Messages = emails}});
// Outputs: "You have 3 new messages"
var addrList = new[] { new { Name = "Jim", Address = new {City = "New York", State = "NY"} } };
Smart.Format("{Name} from {Address.City}, {Address.State}", addrList);
// Outputs: "Jim from New York, NY"
- Fully compatible with, and fully replaces,
String.Format
string.Format("{0} {0:N2} {1:yyyy-MM-dd HH:mm:ss}", 5, new DateTime())
outputs the same results as
Smart.Format("{0} {0:N2} {1:yyyy-MM-dd HH:mm:ss}", 5, new DateTime())
- Extensible plugin architecture. Pluralization, Conditionals, Lists, and Named Placeholders are all bundled plugins. Creating your own is easy, too.
- Pure C# library.
- Fast, small, lightweight. Even with all these features,
Smart.Format
performs nearly as well asstring.Format
-- creating output in microseconds.
It can even out-performstring.Format
: parsing results can be cached, plugins can be disabled, output can be streamed. It's FAST. - Available via Nuget: `Install-Package SmartFormat.NET
- 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