Conditions and lists
Show content only when it applies, repeat it for every item of a list, and control when it is recreated.
janelkit Included with janelkit. Nothing extra to install. 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.
<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>CopyComplete component · Save as src/App.janel in a scaffolded project.
How it works
- count >= 5 is tested first, so five clicks do not also match the second branch.
- {: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.
<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>CopyComplete 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.
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.
- Show the number of items above the list with {len(items)}.
- Empty the list and check that the {:else} text appears.
- 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 ↗.