Skip to main content

LiquidNestingDepth

Identifies Liquid control-flow tags nested deeper than the configured maxDepth. This check counts nested if, unless, for, case, and tablerow tags. elsif and else branches don't add nesting depth.

Reducing nesting depth helps keep theme files easier to scan and makes conditional rendering behavior easier to follow. When a file exceeds the maximum, flatten the logic by combining conditions, returning early with guard clauses, or moving nested logic into snippets that you render from the original file.


The following examples contain code snippets that either fail or pass this check.

In the following example, the innermost if tag is nested 11 levels deep, which is greater than the default maximum of 10:

{% if level_1 %}
{% if level_2 %}
{% if level_3 %}
{% if level_4 %}
{% if level_5 %}
{% if level_6 %}
{% if level_7 %}
{% if level_8 %}
{% if level_9 %}
{% if level_10 %}
{% if level_11 %}
Too deeply nested
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}

In the following example, the nested conditions are flattened into a single guard, so nesting depth stays at one:

{% if level_1 and level_2 and level_3 and level_4 and level_5 and level_6 and level_7 and level_8 and level_9 and level_10 and level_11 %}
Rendered when every condition is met
{% endif %}

The following example contains the default configuration for this check:

LiquidNestingDepth:
enabled: true
severity: warning
maxDepth: 10
ParameterDescription
enabledWhether this check is enabled.
severityThe severity of the check.
maxDepthThe maximum allowed control-flow nesting depth for a Liquid file.

Anchor to Disabling this checkDisabling this check

Disabling this check isn't recommended. If you can't avoid violating the rule, then you should disable the check using the comment syntax. This ensures that you intentionally disable the check for each instance.

{% # theme-check-disable LiquidNestingDepth %}
{% if level_1 %}
{% if level_2 %}
Nested logic
{% endif %}
{% endif %}
{% # theme-check-enable LiquidNestingDepth %}

Was this page helpful?