Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{{#each ...}}...{{else}}...{{/each}} #90

Closed
Rich-Harris opened this issue Dec 1, 2016 · 7 comments
Closed

{{#each ...}}...{{else}}...{{/each}} #90

Rich-Harris opened this issue Dec 1, 2016 · 7 comments

Comments

@Rich-Harris
Copy link
Member

Suggestion via Twitter – an else block for the special case when your array is empty:

{{#each todos as todo}}
  <Todo description='{{todo.description}}' done='{{todo.done}}'/>
{{else}}
  <p>Nothing left to do! Congratulations, turn off your laptop and go outside</p>
{{/each}}
@alexcorvi
Copy link

An else block that is related to iteration? I think that's kind of confusing!

I don't see anything wrong with this:

{{#each todos as todo}}
  <Todo description='{{todo.description}}' done='{{todo.done}}'/>
{{/each}}
{{#if todos.length === 0}}
  <p>Nothing left to do! Congratulations, turn off your laptop and go outside</p>
{{/if}}

Or this:

{{#if todos.length}}
  {{#each todos as todo}}
    <Todo description='{{todo.description}}' done='{{todo.done}}'/>
  {{/each}}
{{else}}
  <p>Nothing left to do! Congratulations, turn off your laptop and go outside</p>
{{/if}}

After all, this is the JavaScript way of doing it.

@Swatinem
Copy link
Member

Swatinem commented Dec 2, 2016

There is a lot of precedent in other template languages for having an else branch for iteration.

@euvl
Copy link

euvl commented Dec 2, 2016

Sounds very convenient and the use-case is quite common, 1+ for each-else

@Ryuno-Ki
Copy link

Ryuno-Ki commented Dec 2, 2016

In django, that cased is triggered in the empty clause of a for loop.

@jadedevin13
Copy link

jadedevin13 commented Dec 3, 2016

Why not this?

{{#each todos as todo}}
  <Todo description='{{todo.description}}' done='{{todo.done}}'/>
{{empty}}
  <p>Nothing left to do! Congratulations, turn off your laptop and go outside</p>
{{/each}}

Laravel uses that syntax, https://laravel.com/docs/5.3/blade#loops.

@kyleshevlin
Copy link

Each/else is a built in helper for Handlebars. http://handlebarsjs.com/builtin_helpers.html

@dux
Copy link

dux commented Apr 24, 2020

needs documentation update in the example?

https://svelte.dev/examples#keyed-each-blocks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants