使用 JsRender 时不输出空行

Do not output empty lines when using JsRender

我在 Node.js 中使用 JsRender
如果给出示例数据:

{
    "items": [
        {"name":"Alf", display:false},
        {"name":"Bruno", display:true}
    ]
}

还有一个模板:

{{* index=0; }}
{{for items}}
{{if display}}
name[{{*: index; }}] = "{{: name }}";
{{* index++; }}
{{/if}}
{{/for}}

我得到:

//empty line
//empty line
//empty line
//empty line
//empty line
name[0] = "Bruno";
//empty line
//empty line
//empty line

如您所见,模板的每一行我都没有输出任何内容,其中有大量空行。
有没有办法阻止 JsRender 输出这些空行?

JsRender 将输出模板中的任何 white-space 个字符。

它猜不出你想要哪些,不想要哪些。 (幽默地说,可以说:把垃圾放进去,把垃圾拿出来!!)。

例如:

{{for items}}{{:name}}{{/for}}

会输出

"AlfBruno"

{{for items}}{{:name}} {{/for}}

会输出

"Alf Bruno "

{{for items}}{{:name}}
{{/for}}

会输出

"Alf\nBruno\n"

所以如果你根本不想换行,你可以写

{{* index=0; }}{{for items}}{{if display}}name[{{*: index; }}] = "{{:name}}";{{* index++; }}{{/if}}{{/for}}

或者如果您愿意

{{* index=0;
}}{{for items
}}{{if display
}}name[{{*: index; }}] = "{{:name}}";{{* index++;
}}{{/if}}{{/for}}