In Mustache templating is there an elegant way of expressing a comma separated list without the trailing comma?

后端 未结 17 1601
滥情空心
滥情空心 2020-12-02 10:26

I am using the Mustache templating library and trying to generate a comma separated list without a trailing comma, e.g.

red, green, blue

17条回答
  •  失恋的感觉
    2020-12-02 10:42

    The question of whether Mustache offers an elegant way to do this has been answered, but it occurred to me that the most elegant way to do this may be to use CSS rather than changing the model.

    Template:

      {{#items}}
    • {{name}}
    • {{/items}}

    CSS:

    .csl li
    {
        display: inline;
    }
    .csl li:before
    {
        content: ", "
    }
    .csl li:first-child:before
    {
        content: ""
    }
    

    This works in IE8+ and other modern browsers.

提交回复
热议问题