GUIDE / FUNDAMENTALS

Conditions and lists

Show content only when it applies, repeat it for every item of a list, and control when it is recreated.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 10 / 29~3 min read · try examples at your own pace
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

What you will learn

  • How {#if} chooses what is shown, and that removed content is destroyed.
  • How {#each} repeats content and why every changing list needs a key.
  • When to recreate content with {#key}, and when to keep it with {#hidden}.

Show content with {#if}

A block decides which part of the template exists. {#if condition} starts a branch, {:else if condition} adds another test, {:else} is the fallback and {/if} closes the block. Conditions use Python truthiness, and only the first matching branch is shown.

janel
<script lang="python">
from janelkit import state
count = state(0)
</script>

<window title="Conditions" width={380} height={220}>
  <column padding={24} gap={12}>
    <button on:click={count += 1}>Click</button>
    {#if count >= 5}
      <text>Five or more clicks</text>
    {:else if count > 0}
      <text>Some clicks</text>
    {:else}
      <text>No clicks yet</text>
    {/if}
  </column>
</window>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

How it works

  1. count >= 5 is tested first, so five clicks do not also match the second branch.
  2. {:else} needs no condition; here it covers zero.

What you should see

No clicks yet at first, Some clicks after the first click, Five or more clicks from the fifth.

A hidden branch does not exist

Only the active branch exists. Switching branches destroys the old content, stops its bindings and creates the new content from scratch. A component inside a removed branch loses its local state; showing it again creates a fresh instance whose script runs again.

Keep data that must survive in the parent, or use {#hidden}, described below.

Repeat content with {#each}

{#each items as item, i (key)} creates one row for every item of an iterable. item is the current element and i, optional, is its position from zero. Both exist only inside the block. The key in parentheses identifies each item, and {:else} is shown when the list is empty.

The rows stay connected to the data: adding, removing or reordering items updates only the affected rows.

janel
<script lang="python">
from janelkit import state
items = state([
    {"id": 1, "name": "Bread"},
    {"id": 2, "name": "Milk"},
])
</script>

<window title="A small list" width={420} height={320}>
  <column padding={24} gap={12}>
    <button on:click={items.reverse()}>Reverse order</button>
    {#each items as item, i (item["id"])}
      <row gap={8}>
        <text>{i + 1}. {item["name"]}</text>
        <button on:click={items.remove(item)}>Remove</button>
      </row>
    {:else}
      <text>The list is empty.</text>
    {/each}
  </column>
</window>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

What you should see

Bread and Milk appear as numbered rows. Reverse order swaps them and renumbers them. Removing both shows The list is empty.

A key is an identity, not a position

Without a key, rows are matched by position. After sorting or deleting, text typed in a field or the keyboard focus can end up on the wrong item. With a key, each row keeps its identity: its local state, its focus and what was typed stay with the item.

Use a value that is unique, stable and hashable, such as an id number or string. If an item changes but keeps its key, its row is updated in place.

Common mistakes

  • Do not use the index i as the key of a list that changes order.
  • Do not generate a random key on each render; that throws identity away.
  • Lists and dictionaries are not hashable and cannot be keys.

Recreate with {#key}, keep with {#hidden}

{#key expression} destroys and recreates its content whenever the expression changes. Use it to reset a component’s local state, for example an editor’s draft when another note is selected.

{#hidden condition} is the opposite of {#if}: it hides its content while the condition is true without destroying it. When shown again, the content keeps what was typed, its scroll position and its state. It is created the first time it is shown. While hidden, its template stops updating and catches up once when shown, but its script keeps running: effects, timers and tasks continue.

janel
<!-- selected_id and current come from the script; tab is a text state. -->
{#key selected_id}
  <NoteEditor note={current} />
{/key}

{#hidden tab != "search"}
  <SearchPanel />
{/hidden}
Copy

Fragment · Add it to an existing component; it is not a complete file.

How it works

  1. Choosing another note changes selected_id, so NoteEditor is recreated with an empty draft.
  2. Switching tabs only hides SearchPanel; coming back finds the search exactly as it was.

Common mistakes

  • Use {#hidden} for content that is toggled often and is costly to build or has state that matters. For everything else, {#if} frees memory.

Several elements in one branch or row

When a branch or a row contains several elements, they are grouped in an implicit container with the parent’s direction, gap and alignment. Usually this is invisible; if you need to control their layout, wrap them in your own <column> or <row>. Blocks go inside the root element, never around it.

  1. Show the number of items above the list with {len(items)}.
  2. Empty the list and check that the {:else} text appears.
  3. Type into a field in a keyed row, reverse the list, and confirm the text moved with its item.

Key points

  • {#if} shows one branch; removed content is destroyed.
  • {#each} repeats content; give changing lists a stable key.
  • {#key} recreates content; {#hidden} keeps it alive while hidden.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.