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

后端 未结 17 1619
滥情空心
滥情空心 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:07

    There is not a built-in way to do this in Mustache. You have to alter your model to support it.

    One way to implement this in the template is to use the inverted selection hat {{^last}} {{/last}} tag. It will only omit text for the last item in the list.

    {{#items}}
        {{name}}{{^last}}, {{/last}}
    {{/items}}
    

    Or you can add a delimiter string as ", " to the object, or ideally the base class if you're using a language that has inheritance, then set "delimiter" to an empty string " " for the last element like this:

    {{#items}}
        {{name}}{{delimiter}}
    {{/items}}
    

提交回复
热议问题