Skip to content

Template logic

Anup Chavan edited this page Jun 21, 2026 · 1 revision

Template logic

Template logic lets you include markup conditionally, loop over arrays, and assign local variables. The syntax is inspired by Obsidian Web Clipper, Liquid, and Twig, but Custom Views has its own supported behavior.

If blocks

{% if rating >= 8 %}
  <span class="badge">Favorite</span>
{% endif %}

Use else for fallback markup:

{% if description %}
  <p>{{description}}</p>
{% else %}
  <p>No description yet.</p>
{% endif %}

Use elif for chained conditions:

{% if rating >= 9 %}
  <span>Masterpiece</span>
{% elif rating >= 7 %}
  <span>Recommended</span>
{% else %}
  <span>Unrated</span>
{% endif %}

Conditions

Common comparison and logical operators:

Operator Meaning
== Equal.
!= Not equal.
>, <, >=, <= Numeric or comparable values.
and, && Both sides must be true.
or, `
not, ! Negation.
?? Fallback value.

Examples:

{% if artists.length == 1 or artists.contains(albumArtist) %}
  <span class="single-artist">{{albumArtist}}</span>
{% endif %}

{{subtitle ?? "Untitled"}}

Loops

Loop over arrays from frontmatter:

<ul>
{% for artist in artists %}
  <li>{{artist}}</li>
{% endfor %}
</ul>

Loop over Bases rows:

{% for row in bases.Songs.rows %}
  <div>{{row.values.track}}. {{row.file.link}}</div>
{% endfor %}

Loop variables

Inside a loop, Custom Views exposes a loop object:

Variable Meaning
loop.index 1-based index.
loop.index0 0-based index.
loop.first True on the first item.
loop.last True on the last item.
loop.length Total item count.

Example:

{% for genre in genres %}
  <span>{{genre}}</span>{% if not loop.last %}<span> · </span>{% endif %}
{% endfor %}

Set variables

Use set when a value is reused or when a condition becomes easier to read.

{% set title = file.basename.replace(/.*\s*–\s*/, "") %}
<h1>{{title}}</h1>

For album tracks, this avoids repeating artist logic:

{% set trackArtists = row.values.artists %}
{% if trackArtists.length > 1 and not trackArtists.contains(artists[0]) %}
  <div class="album-track-artists">{{trackArtists}}</div>
{% endif %}

A common gotcha: filters inside if

This is output-filter syntax and may not behave the way you expect inside conditions:

{% if row.values.artists|length > 1 %}
  ...
{% endif %}

Prefer expression syntax:

{% if row.values.artists.length > 1 %}
  ...
{% endif %}

For robust album artist hiding:

{% set trackArtists = row.values.artists %}
{% if trackArtists.length > 1 and not trackArtists.contains(artists[0]) %}
  <div class="album-track-artists">{{trackArtists}}</div>
{% endif %}

Wiki pages

Clone this wiki locally