Skip to content

[feature request] v-empty for v-for #4174

@gbezyuk

Description

@gbezyuk

It's a common issue when rendering lists to show a message in case the list is empty. In Django, for example, {% for %} template tag supports {% empty %} for this:

{% for platform in game.platform_set.all %}       
    <td>{{ platform.system }} -- ${{ platform.price}}</td> 
{% empty %}
    <td>No Platforms</td>
{% endfor %}

Probably we could benefit from the similar feature in Vue.

There are cases with the list being contained inside an element to be hidden when the list is empty, and it works just fine with Vue if we want to hide the whole container:

<ul v-if="items.length">
  <li v-for="item in items">
    {{ item.message }}
  </li>
</ul>
<p v-else>Empty list</p>

When it's not the case, however, the syntax is getting a little verbose. Emulating the Django example mentioned above:

<td v-for="platform in game.platforms" v-if="game.platforms.length">
  {{ platform.system }} -- ${{ platform.price }}
</td>
<td v-else>No Platforms</p>

we could benefit from a simplification, getting rid of expicit v-if:

<td v-for="platform in game.platforms">
  {{ platform.system }} -- ${{ platform.price }}
</td>
<td v-else>No Platforms</p>

UPD: v-empty could form a nice name as well, in case we'd like to avoid conflicts with existing v-else implementation.

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