if() : Ternary Conditional
On this page
Summary
Evaluate a condition and return one value if true, another if false. Similar to the ternary operator in programming languages.
Signature
if(condition, trueValue, falseValue)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
condition |
Boolean | Yes | Expression that evaluates to true/false |
trueValue |
Any | Yes | Value to return if condition is true |
falseValue |
Any | Yes | Value to return if condition is false |
Returns
Type: Any (type of trueValue or falseValue)
Returns trueValue if condition is true, falseValue otherwise.
Examples
Basic Usage
Conditional CSS Class
Data:
doc.Params["model"] = new {
name = "John Doe",
isPremium = true
};
Output:
<div class="premium">
<h3>John Doe</h3>
</div>
With Comparison
Nested Conditions
With Special Variables
Avoid Division by Zero
Notes
- Both
trueValueandfalseValueare evaluated (no short-circuit) - Can be nested for complex logic
- Consider using `` helper for cleaner multi-line conditionals
- Useful for inline conditional values