Skip to content
axunonb edited this page Feb 7, 2022 · 110 revisions

What is SmartFormat?

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.

Show me some examples

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.

Clone this wiki locally