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

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

    Hrm, doubtful, the mustache demo pretty much shows you, with the first property, that you have to have the logic inside the JSON data to figure out when to put the comma.

    So your data would look something like:

    {
      "items": [
        {"name": "red", "comma": true},
        {"name": "green", "comma": true},
        {"name": "blue"}
      ]
    }
    

    and your template

    {{#items}}
        {{name}}{{#comma}},{{/comma}}
    {{/items}}
    

    I know it's not elegant, but as mentioned by others Mustache is very lightweight and does not provide such features.

提交回复
热议问题