-
-
Notifications
You must be signed in to change notification settings - Fork 105
Condition _ ConditionalFormatter
The ConditionalFormatter
has a lot of functionality for shaping the output string based on conditions.
The behavior of ConditionalFormatter
varies depending on the data type of the placeholder.
{ Any Value : cond : output 1 | output 2 | output n | default }
Value | formatter name | outputs | default |
---|---|---|---|
Any value | "cond" | list of choices | (optional) output if nothing matched |
The number of outputs including default must always have at least 2 choices.
string Name
: default iscond
The name to use a named formatter
char SplitChar
: default is'|'
Number: can be short
, ushort
, int
, uint
, long
, ulong
, float
, double
, decimal
The value of the number determins the index in the list choices. If the value is not an integer, the largest integer less than or equal to the specified value is used.
Negative values, or values exceeding the number of choices will output the default.
Smart.Format("{0:cond:Apple|Pie|Orange|Banana|No fruit}", arg);
/* Outputs:
arg == 0: "Apple"
arg == 3: "Banana"
arg < 0 || arg > 3: "No fruit"
*/
Output options:
cond1 ? output cond1 | cond-n ? output cond-n
Each option is separated by the SplitChar
. The comparison is followed by a "?" and then the text for a match. The last (default) entry neither contains a comparison, nor a "?". Valid comparisons: >=
>
=
==
<
<=
!=
.
Comparisons can be combined using "&" for AND or "/" for OR.
var age = 32;
var arg = new KeyValuePair<string, object>("Age", age);
Smart.Format("{Age:cond:<=0?Not yet born?|>=55?Senior Citizen|>=30?Adult|>=18?Young Adult|>12?Teenager|>2?Child|Baby}", arg);
/* Outputs, depending on age:
32: "Adult"
70: "Senior Citizen"
2: "Baby"
0: "Not yet born?"
*/
true
uses the first, false
uses the second item of the output choices.
Smart.Format("{0:cond:Sun|Moon}", arg);
/* Outputs:
arg == true: "Sun"
arg == false: "Moon"
*/
Outputs the value, if it's not null
or string.Empty
, else the default choice.
Smart.Format("{0:cond:{}|Null or Empty}", arg);
/* Outputs:
arg == "smart": "smart"
arg == null: "Null or Empty"
arg == "": "Null or Empty"
*/
Compares the value to the current calender date (year, month, day).
With 3 output choices, the index is 0 for past, 1 for present, 2 for future. With 2 output choices, the index is 0 for present or past, 1 for future.
Smart.Format("{0:cond:yesterday|today|tomorrow}", arg);
/* Outputs:
arg == DateTime.Now.AddDays(-1): "yesterday"
arg == DateTime.Now: "today"
arg == DateTime.Now.AddDays(-1): "tomorrow"
*/
Compares the value to TimeSpan.Zero
.
With 3 output choices, the index is 0 for negative, 1 for zero, 2 for positive. With 2 output choices, the index is 0 for negative or zero, 1 for positive.
Smart.Format("{0:cond:{Hours} hours ago|now|{Hours} hours later}", arg);
/* Outputs:
arg == TimeSpan.Zero.Add(new TimeSpan(-2,0,0)): "2 hours ago"
arg == TimeSpan.Zero: "now"
arg == TimeSpan.Zero.Add(new TimeSpan(3,0,0)): "3 hours later"
*/
- 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