For the complete documentation index, see llms.txt. This page is also available as Markdown.

@if

Display content based on dynamic conditions

Conditional statements in Elements allow you to control when content is displayed based on certain conditions. Using @if, @elseif, and @else, you can define different outcomes depending on the values of variables. This is useful for showing or hiding content based on user settings, mode (edit or preview), or other dynamic conditions.

Syntax

@if(condition)
    <!-- Content shown when condition is true -->
@endif

With else if and else branches:

@if(condition)
    <!-- Content for first condition -->
@elseif(otherCondition)
    <!-- Content for second condition -->
@else
    <!-- Fallback content -->
@endif

Block-Level Conditionals

Basic If Statement

Display content when a switch or boolean property is true:

If Not Statement

Use the ! operator to check if something is false:

Multiple Conditions with Else If

Chain multiple conditions before falling back to else:

Edit/Preview Mode Detection

Elements provides built-in edit and preview variables:

Inline Conditionals

Conditionals can also be used inline within HTML attributes. This is particularly useful for dynamic class names and attribute values.

Dynamic Class Names

Apply classes conditionally based on properties:

Multiple Conditional Classes

Chain multiple inline conditions for complex class logic:

Conditional Attributes

Add or remove attributes based on conditions:

Conditional Image Attributes

Apply different behaviors based on settings:

Conditional Visibility Styles

Hide elements in preview/published mode:

Nested Conditionals

Conditionals can be nested for complex logic:

Combining with @each

Use conditionals within loops for filtered rendering:

Common Patterns

Fallback Content

Show a placeholder when no content exists:

Feature Toggles

Enable or disable component features:

Conditional Includes

For conditional template includes, consider using @includeIf for cleaner syntax:

Combining Conditions

Because a template @if only accepts a single condition, you cannot write expressions like @if(edit && hidePreview) or @if(count > 0). Use one of the following approaches instead.

Nest separate @if directives

For "AND" logic, nest one @if inside another:

Push the logic into CSS

Emit marker classes from independent @if directives, then combine them with a CSS selector. This is the recommended pattern for show/hide behaviour, as it works reliably in edit, preview, and published modes:

Compute a boolean in hooks.js

For comparisons or more complex logic, calculate a single boolean in the hooks file and pass it to the template, then test that one value:

Best Practices

  1. One condition per @if - Template @if takes a single condition (optionally negated with !). For combined or comparison logic, nest @if directives, combine marker classes in CSS, or compute a boolean in the hooks file. See Combining Conditions.

  2. Keep conditions simple - For complex logic like string comparisons, use the hooks file to compute boolean values and pass them to the template.

  3. Use meaningful property names - Boolean properties should have clear names like showIcon, enableLazyLoading, or hasImage.

  4. Prefer @includeIf for conditional includes - Instead of wrapping @include in @if, use @includeIf for cleaner templates.

  5. Only non-responsive controls work in conditionals - Responsive controls cannot be used directly in @if statements.

Last updated

Was this helpful?