Skip to content
Norbert Bietsch edited this page Dec 3, 2016 · 110 revisions

SmartFormat.NET

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: Most of the examples in this Wiki are using "Smart.Format(...)". This is for a good reason: Smart.Format() automatically initializes the SmartFormatter with default extensions. If you're using SmartFormatter.Format() directly, then it's your job to initialize. So normally just leave SmartFormatter.Format() alone.

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"

[Named placeholders](Expressions and Nesting)

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"

Extra features

  • 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 as string.Format -- creating output in microseconds.
    It can even out-perform string.Format: parsing results can be cached, plugins can be disabled, output can be streamed. It's FAST.
  • Available via Nuget: `Install-Package SmartFormat.NET
Clone this wiki locally