Skip to content

Suggestion: "v-glue" (v-for element joins) #1841

@furey

Description

@furey

When v-for lists are rendered, they do so without any space between elements.

For example:

<ol class="breadcrumb">
    <li v-for="n in 3"><a>Crumb {{ n+1 }}</a></li>
</ol>

renders as:

<ol class="breadcrumb">
    <li><a>Crumb 1</a></li><li><a>Crumb 2</a></li><li><a>Crumb 3</a></li>
</ol>

Because of this, and due to a known issue in Bootstrap, the above breadcrumb's separator spacing will display incorrectly (note the spacing on the left of a separator is smaller than on the right):

rendered-breadcrumb

To work around this (I'm writing a breadcrumb component), I run the v-for loop in a <template> wrapper so I can append a <slot> containing a blank space after each <li> element, like so:

<ol class="breadcrumb">
    <template v-for="n in 3">
    <li><a>Crumb {{ n+1 }}</a></li>
    <slot> </slot>
    </template>
</ol>

which renders as:

<ol class="breadcrumb">
    <li><a>Crumb 1</a></li>
    <li><a>Crumb 2</a></li>
    <li><a>Crumb 3</a></li>

</ol>

and corrects the separator spacing problem.

To avoid such a workaround, I was wondering if there's any interest in the idea of a v-glue directive for v-for loops, as a way to pass an expression/value to the loop to be used as the glue that joins each item of the loop together?

For example, the problem above could be negated by simply passing v-glue=" " like so:

<ol class="breadcrumb">
    <li v-for="n in 3" v-glue=" ">Crumb {{ n+1 }}</li>
</ol>

which would render as (note the spaces between the <li> tags):

<ol class="breadcrumb">
    <li><a>Crumb 1</a></li> <li><a>Crumb 2</a></li> <li><a>Crumb 3</a></li>
</ol>

I can imagine other creative applications of such a feature, e.g. dividers between panels, etc.

Interested to hear your thoughts!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions