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

后端 未结 17 1620
滥情空心
滥情空心 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 11:04

    As the question is:

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

    Then changing the data - when being the last item is already implicit by it being the final item in the array - isn't elegant.

    Any mustache templating language that has array indices can do this properly,. ie. without adding anything to the data. This includes handlebars, ractive.js, and other popular mustache implementations.

    {{# names:index}}
        {{ . }}{{ #if index < names.length - 1 }}, {{ /if }}
    {{ / }}
    

提交回复
热议问题