Skip to content

Template syntax for let bindings #1521

@girving

Description

@girving

Currently there is no specific syntax to reuse computations that depend on each loop variables. For example, here's some code that loops over some numbers and uses the squares of the numbers in two different ways:

{#each [1,2,3,4] as n}
  {#each [n*n] as sqr_n}
    {sqr_n} {sqr_n/2}<br>
  {/each}
{/each}

The inner each is a hack to define a reusable computation. It would lovely to have syntax something like this:

{#each [1,2,3,4] as n}
  {#let sqr_n = n * n}
  {sqr_n} {sqr_n/2}<br>
{/each}

Besides being easier to read, that syntax has the advantage of not introducing another nesting level. Is there a clean alternative to the second version in current svelte? If not, would this be a reasonable feature to add?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions