Javascript 将字符串列表转换为带小胡子的html列表

Javascript 将字符串列表转换为带小胡子的html列表,javascript,mustache,Javascript,Mustache,创建带有小胡子的列表: 有可能处理一组有胡子的字符串吗 请注意:我不是问如何将数组转换为兼容格式。取而代之的是,我想给它留胡子。来自: 在字符串数组上循环时,一个。可用于引用列表中的当前项。 视图: { “火枪手”:[“阿托斯”、“阿拉米斯”、“波托斯”、“达塔尼安”] } 模板: {#火枪手} * {{.}} {{/火枪手}} 所以在你的情况下,你只需要写: {{{#repo} {{.}} {{/repo} 这是特定于mustache.js的吗?我在小胡子里找不到 const Mustache

创建带有小胡子的列表:

有可能处理一组有胡子的字符串吗

请注意:我不是问如何将数组转换为兼容格式。取而代之的是,我想给它留胡子。

来自:

在字符串数组上循环时,一个。可用于引用列表中的当前项。 视图:

{
“火枪手”:[“阿托斯”、“阿拉米斯”、“波托斯”、“达塔尼安”]
}

模板:

{#火枪手}
* {{.}}
{{/火枪手}}

所以在你的情况下,你只需要写:

{{{#repo}
{{.}}
{{/repo}

这是特定于mustache.js的吗?我在小胡子里找不到
const Mustache = require('mustache');

let template = `
    {{#repo}}
        <b>{{name}}</b>
    {{/repo}}
`

let context={
    repo:[
        {name: "John"},
        {name: "Lisa"},
        {name: "Darth Vader"}
    ]
}
console.log(Mustache.render(template, context))
<b>John</b>
<b>Lisa</b>
<b>Darth Vader</b>
// my data is just a list of strings
let my_data = ["a", "b", "c", "d"]

// to make it compatible with Mustache, it has to become a list of objects
let mustache_version=[{el: "a"}, {el: "b"}, {el: "c"}, {el: "d"}]