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

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

    I tend to think this is a task well suited to CSS (as answered by others). However, assuming you are attempting to do something like produce a CSV file, you would not have HTML and CSS available to you. Also, if you are considering modifying data to do this anyway, this may be a tidier way to do it:

    var data = {
      "items": [
        {"name": "red"},
        {"name": "green"},
        {"name": "blue"}
      ]
    };
    
    // clone the original data. 
    // Not strictly necessary, but sometimes its
    // useful to preserve the original object
    var model = JSON.parse(JSON.stringify(data));
    
    // extract the values into an array and join 
    // the array with commas as the delimiter
    model.items = Object.values(model.items).join(',');
    
    var html = Mustache.render("{{items}}", model);
    
    0 讨论(0)
  • 2020-12-02 11:00

    In case using Handlebars is an option, which extends the capabilities of Mustache, you could use a @data variable:

    {{#if @last}}, {{/if}}
    

    More info: http://handlebarsjs.com/reference.html#data

    0 讨论(0)
  • 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 }}
    {{ / }}
    
    0 讨论(0)
  • 2020-12-02 11:05

    I can't think of many situations where you'd want to list an unknown number of items outside of a <ul> or <ol>, but this is how you'd do it:

    <p>
        Comma separated list, in sentence form;
        {{#each test}}{{#if @index}}, {{/if}}{{.}}{{/each}};
        sentence continued.
    </p>
    

    …will produce:

    Command separated list, in sentence form; asdf1, asdf2, asdf3; sentence continued.
    

    This is Handlebars, mind you. @index will work if test is an Array.

    0 讨论(0)
  • 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}}
    
    0 讨论(0)
提交回复
热议问题