Quarto Conditional Vars

Active variables

Code:

{{< show-vars >}}

Result:

  • active: true
  • region: south
  • report.audience: public
  • report.level: 2
  • support_moderate_pct: 58
  • support_pct: 58
  • support_strong_pct: 82
  • support_weak_pct: 28
  • type: detailed
  • year: 2024

Exact match — .when-var

Code:

::: {.when-var type="detailed"}
Shown only when `type` equals `detailed`.
:::

Result:

Shown only when type equals detailed.

Code:

::: {.when-var type="summary"}
Shown only when `type` equals `summary`. *(should not appear)*
:::

Result:


Inverse — .unless-var

Code:

::: {.unless-var region="north"}
Shown in every region except `north`.
:::

Result:

Shown in every region except north.

Code:

::: {.unless-var region="south"}
Shown in every region except `south`. *(should not appear)*
:::

Result:


AND condition — multiple attributes

Code:

::: {.when-var type="detailed" region="south"}
Shown only when `type` is `detailed` **and** `region` is `south`.
:::

Result:

Shown only when type is detailed and region is south.


OR condition — pipe-separated values

Code:

::: {.when-var type="detailed|summary"}
Shown when `type` is `detailed` **or** `summary`.
:::

Result:

Shown when type is detailed or summary.

Code:

::: {.when-var type="summary|draft"}
Shown when `type` is `summary` **or** `draft`. *(should not appear)*
:::

Result:

Code:

::: {.when-var year=">2025|<2020"}
Shown when `year` > 2025 **or** `year` < 2020. *(should not appear)*
:::

Result:


Mixed — .when-var + .unless-var

When both classes are present, use when-<var> and unless-<var> prefixes to declare each condition explicitly.

Code:

::: {.when-var .unless-var when-type="detailed" unless-region="north"}
Shown when `type` is `detailed` **and** `region` is not `north`.
:::

Result:

Shown when type is detailed and region is not north.

Code:

::: {.when-var .unless-var when-type="detailed" unless-region="south"}
Shown when `type` is `detailed` **and** `region` is not `south`. *(should not appear)*
:::

Result:


Boolean variable

Code:

::: {.when-var active="true"}
Shown only when `active` is `true`.
:::

Result:

Shown only when active is true.

Code:

::: {.when-var active="false"}
Shown only when `active` is `false`. *(should not appear)*
:::

Result:


Numeric conditions

Code:

::: {.when-var year=">2020"}
Shown when `year` > 2020.
:::

Result:

Shown when year > 2020.

Code:

::: {.when-var year=">=2024"}
Shown when `year` >= 2024.
:::

Result:

Shown when year >= 2024.

Code:

::: {.when-var year="<2020"}
Shown when `year` < 2020. *(should not appear)*
:::

Result:

Code:

::: {.when-var year="<=2023"}
Shown when `year` <= 2023. *(should not appear)*
:::

Result:

Code:

::: {.when-var year=">=2020&<2025"}
Shown when `year` is in [2020, 2025). 
:::

Result:

Shown when year is in [2020, 2025).

Code:

::: {.when-var year=">=2025&<2030"}
Shown when `year` is in [2025, 2030). *(should not appear)*
:::

Result:

Code:

::: {.when-var year=">=2020&<2023|>=2024&<2026"}
Shown when `year` is in [2020, 2023) **or** [2024, 2026).
:::

Result:

Shown when year is in [2020, 2023) or [2024, 2026).

Code:

::: {.when-var year=">=2010&<2015|>=2030&<2035"}
Shown when `year` is in [2010, 2015) **or** [2030, 2035). *(should not appear)*
:::

Result:


Inline conditionals

Code:

Inline: [visible only when `type` is `detailed`]{.when-var type="detailed"}.

Result:

Inline: visible only when type is detailed.

Code:

Inline: [hidden when `region` is `south`]{.unless-var region="south"}. *(should not appear)*

Result:

Inline: .

Code:

Inline OR: [visible for `south` or `north`]{.when-var region="south|north"}.

Result:

Inline OR: visible for south or north.

Code:

Inline numeric: [shown when year is >= 2024]{.when-var year=">=2024"}.

Result:

Inline numeric: shown when year is >= 2024.

Code:

Inline numeric range (50% <= support_pct < 60%):
[in range]{.when-var support_pct=">=50&<60"}

Equivalent form:
[in range]{.when-var .unless-var when-support_pct=">=50" unless-support_pct=">=60"}

Result:

Inline numeric range (50% <= support_pct < 60%): in range

Equivalent form: in range

Code:

Support (58):
[weak support]{.when-var support_pct="<40"}
[moderate support]{.when-var .unless-var when-support_pct=">=40" unless-support_pct=">=70"}
[strong support]{.when-var support_pct=">=70"}

Result:

Support (58): moderate support

Code:

Support with color (58%):
[**weak**]{.when-var support_pct="<40" style="color: #cc0000;"}
[**moderate**]{.when-var .unless-var when-support_pct=">=40" unless-support_pct=">=70" style="color: #e6b800;"}
[**strong**]{.when-var support_pct=">=70" style="color: #2e7d32;"}

Result:

Support with color (58%): moderate

Code:

Front comparison with multiple numeric supports:

Scenario A (28%):
[weak support]{.when-var support_weak_pct="<40"}
[moderate support]{.when-var .unless-var when-support_weak_pct=">=40" unless-support_weak_pct=">=70"}
[strong support]{.when-var support_weak_pct=">=70"}

Scenario B (58%):
[weak support]{.when-var support_moderate_pct="<40"}
[moderate support]{.when-var .unless-var when-support_moderate_pct=">=40" unless-support_moderate_pct=">=70"}
[strong support]{.when-var support_moderate_pct=">=70"}

Scenario C (82%):
[weak support]{.when-var support_strong_pct="<40"}
[moderate support]{.when-var .unless-var when-support_strong_pct=">=40" unless-support_strong_pct=">=70"}
[strong support]{.when-var support_strong_pct=">=70"}

Result:

Front comparison with multiple numeric supports:

Scenario A (28%): weak support

Scenario B (58%): moderate support

Scenario C (82%): strong support


Nested variables

Variables defined as nested mappings in _variables.yml are accessible with dot notation.

_variables.yml:

report:
  audience: public
  level: 2

Code:

::: {.when-var report.audience="public"}
Shown only when `report.audience` is `public`.
:::

Result:

Shown only when report.audience is public.

Code:

::: {.when-var report.level=">=2"}
Shown when `report.level` >= 2.
:::

Result:

Shown when report.level >= 2.


Unknown variables emit a warning

A reference to a variable that does not exist evaluates to false (block hidden) and prints a warning to the console:

Code:

::: {.when-var country="Brazil"}
Shown only when `country` equals `Brazil`. *(should not appear)*
:::

Result:

Console warning:

WARNING [conditional-vars]: unknown variable ‘country’ referenced in .when-var / .unless-var


How to call variables in Quarto

TipCanonical way to use variables in Quarto

Code:

{{< var active >}}

Result:

true

This approach is recommended especially for text-focused workflows, because it keeps variables in the document’s metadata layer rather than the execution layer.