Skip to main content Link Search Menu Expand Document (external link)

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


<!-- Output: Active (if isActive is true) -->

Conditional CSS Class

<div class="">
  <h3></h3>
</div>

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


  <tr class="">
    <td></td>
  </tr>

Avoid Division by Zero

<p>Average: </p>

Notes

  • Both trueValue and falseValue are evaluated (no short-circuit)
  • Can be nested for complex logic
  • Consider using `` helper for cleaner multi-line conditionals
  • Useful for inline conditional values

See Also