Conditional Rendering

Conditional rendering allows your template to adapt based on the presence or absence of field values. Mochi uses Mustache-style syntax (<<#field>> … <</field>>) to show or hide parts of your template dynamically.

This makes templates more flexible and avoids displaying empty sections.

Why conditional rendering matters

Conditional rendering lets you:

  • Hide optional fields that aren’t filled
  • Show fallback text when fields are empty
  • Override fields based on priority
  • Display or hide parts of the template with a checkbox
  • Build advanced templates without duplication

Basic conditional syntax

Show a section only if a field has a value:

<<#field_name>>
This appears only when `field_name` is filled.
<</field_name>>

Show a section only if a field is empty:

<<^field_name>>
This appears only when `field_name` is empty.
<</field_name>>

Example use cases

Field fallback (override logic)

Suppose a card may include a custom pronunciation override. If no override exists, fall back to reading:

**Pronunciation:**  
<<#pronunciation_override>>
<<pronunciation_override>>
<</pronunciation_override>>

<<^pronunciation_override>>
<<reading>>
<</reading>>

This lets you override values only when needed.

Adding conditional labels

<<#notes>>
### Notes
<<notes>>
<</notes>>

Labels only appear when the content is present.

Adding a tag when a checkbox is checked

Imagine a deck where some cards should be tagged #important whenever a checkbox field named important is checked.

You can achieve this by using a conditional block to emit the tag only when the box is enabled:

# <<title>>

<<details>>

<<#important>>
#important
<</important>>

When the important checkbox is checked on a card, the template will render the #important tag; if it is unchecked, the tag will be omitted.