Conditional formatter
Select one of several outputs based on the type and value of a placeholder.
Read time 3 minutesLast updated 14 days ago
Select one of several outputs based on the type and value of a placeholder.
The Conditional formatter splits a format into choices and picks one based on the placeholder value. Apply it explicitly with the name , or omit the name to apply it implicitly when the format contains a split character. It interprets the value differently depending on its type: numbers, booleans, strings, dates, and time spans each have their own rules.
condSeparate the choices with the split character. The default is , but and are also valid.
|,~Each choice is itself a format, so it can contain nested placeholders. An empty placeholder reuses the current value.
{}Numbers
For a number, the value selects the choice at that index. Non-integer values are rounded down (floored). A negative value, or a value past the last choice, selects the last choice.
Smart String | Argument | Result |
|---|---|---|
| | Apple |
| | Banana |
| | No fruit |
| | other |
Complex comparisons
You can define more complex comparisons using operators. Use to separate the comparison and its choice. The final choice has no comparison and acts as the fallback when none match.
?The complex comparison syntax only applies to convertible numeric values.
The following comparison operators are supported:
Operator | Description |
|---|---|
| greater than or equal to |
| greater than |
| equal to |
| less than |
| less than or equal to |
| not equal to |
Combine comparisons with for AND or for OR.
&/Smart String | Argument | Result |
|---|---|---|
| | Less than 10 |
| | Adult |
Booleans
For a boolean, the first choice is used when the value is and the second when it is .
truefalseSmart String | Argument | Result |
|---|---|---|
| | Enabled? Yes. |
| | Enabled? No. |
Strings
For a string, the first choice is used when the value is not null or empty; otherwise the second choice is used.
Smart String | Argument | Result |
|---|---|---|
| | The string is not empty |
| | The string is empty |
| | Text: Hello World |
| | Text: No text |
DateTime and DateTimeOffset
For a or , the value is compared to the current date in UTC. Use three choices for past, today, and future indices. Use two choices for today-or-past and future.
DateTimeDateTimeOffsetSmart String | Argument | Result |
|---|---|---|
| a past date | My birthday was yesterday |
| today | My birthday is today |
| a future date | My birthday will be tomorrow |
TimeSpan
For a , the value is compared to . Use three choices for negative, zero, and positive indices. Use two choices for negative-or-zero and positive indices.
TimeSpanTimeSpan.ZeroSmart String | Argument | Result |
|---|---|---|
| a negative duration | The event was 2 hours ago |
| | The event is now |
| a positive duration | The event will start in 3 hours |
Other objects
For any other type, the value is compared to null: the first choice is used when it is not null, the second when it is.
Smart String | Argument | Result |
|---|---|---|
| an object | not null |
| | null |