-
-
Notifications
You must be signed in to change notification settings - Fork 104
Syntax, Terminology
A Format String is the template that defines how the data should be formatted.
Let's analyze the following Format String:
"The user {Name} was born in {Birthday:MMMM}, is {Age:000} {Age:year|years} old, and lives in {Address.City}."
Placeholders are defined by {
and }
, so this example has 5 placeholders: {Name}
, {Birthday:MMMM}
, {Age:000}
, {Age:year|years}
, and {Address.City}
.
Literal text is the area in-between placeholders: The user
, was born in
, , is
,
, old, and lives in
.
Each placeholder starts with a list of Selectors, such as Name
and Birthday
.
Selectors are separated by a period .
or ?.
for nullable, known as an Operator, such as Address
.City
.
Selectors determine what data will be used in the output.
If the placeholder contains a colon :
, the remaining text is known as the Item Format.
For example, MMMM
, 000
, and year|years
are Item Formats.
Note that there is a distinction between the entire Format String and the placeholder's Item Format.
The Item Format determines how that data will be formatted. For example, MMMM
tells the DateTime to output the name of the Month, like January
. 000
tells an integer to use 3 digits.
Literal text { Selector(s) [, Align ] [: Formatter-Name(Options) ] [: Item-Format ] }
Parts within square braces [
and ]
are optional.
var data = new { Item = 9 };
Smart.Format("There are {Item,7:default:000} items", data);
// Outputs: "There are 009 items"
-
Placeholder is everything between
{
and}
curly braces. Each placeholder starts with a list of Selectors. -
Selector(s) is
Item
, which evaluatesdata.Item
and gives us9
. Selectors can be concatenated with a dot.
operator between them ("dot notation"), or?.
for nullable. Selectors determine what data will be used in the output. -
Align is
7
, which causes the output to be padded with 4 extraFormatterSettings.AlignmentFillCharacter
s, which defaults to blank. -
Formatter Name is
"default"
, which explicitly uses theDefaultFormatter
. If omitted, a matching formatter flagged withCanAutoDetect=true
will be chosen implicitly. -
Item Format is
"000"
, which causes the number9
to be padded with zeros. Note that there is a distinction between the entire Format String and the Placeholder's Item Format. - Options are used as arguments to a format extension. options must be surrounded by parenthesis. See the example below.
It's as simple as:
using SmartFormat;
Smart.Format("{0} {1}", "Hello", "World")
// outputs "Hello World"
Smart.Format("{h} {w}", new{ h = "Hello", w = "World" })
// outputs "Hello World"
// More than 1 selector
Smart.Format("{0.ToUpper} {1.ToLower}", "Hello", "World")
// outputs "HELLO world"
Using options for the PluralLocalizationFormatter
:
Smart.Format("{0:plural(en):zero|one|many}", 1);
// outputs "one" for the English language option
Literal text { selector[n] }
or
Literal text { selector.n }
Example:
var data = new {Numbers = new List<int>{0,1,2,3}};
Smart.Format("{Numbers[1]}", data);
Smart.Format("{Numbers.1}", data);
// both output "1"
-
placeholder is everything between
{
and}
curly braces. -
selector is
Number
, which evaluatesdata.Number
-
operators are either
[
]
or.
to access an item in the list, where 1 for "n" is the list index.
Nullable notation syntax is equivalent to C# Nullable<T>
.
Use it in the format string, whenever a selector may be null
. By default null
will output string.Empty
.
Literal text { selector?.selector }
If any of the selectors is null
, the placeholder is evaluated as null
.
Literal text { selector?[n] }
If the selector is null
, the placeholder is evaluated as null
.
public class Person
{
public string? Name {get; set;}
public DateTime? Birthday {get; set;}
}
var data = new Person { Name = "Joe", Birthday = new DateTime(2000, 6, 30) };
Smart.Format("Name: {Name?.ToUpper} - Birthday: {Birthday?.Date:yyyy-MM-dd}", data);
// Outputs: "Name: JOE - Birthday: 2000-06-30"
data = new Person { Name = null, Birthday = null };
Smart.Format("Name: {Name?.ToUpper} - Birthday: {Birthday?.Date:yyyy-MM-dd}", data);
// Outputs: "Name: - Birthday:"
// Or nicer by using the IsNullFormatter:
Smart.Format("Name: {Name?.ToUpper:isnull:n/a|{}} - Birthday: {Birthday?.Date:isnull:n/a|{:yyyy-MM-dd}}", data)
// Outputs: "Name: n/a - Birthday: n/a"
Smart.Format("Name: {Name.ToUpper} - Birthday: {Birthday.Date:yyyy-MM-dd}", data);
// Throws a FormattingException
// Name: {Name.ToUpper}
// ------------^
- 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