如何删除Handlebars生成的换行符? [英] How to remove newlines generated by Handlebars?

查看:92
本文介绍了如何删除Handlebars生成的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有如下的模板:

Suppose I have a template like the following:

start
{{#if data}}
data
{{/if}}
end

不管什么

start

data

end

有没有办法让Handlebars不生成换行符正在占领(不移动标签本身)?例如

Is there a way to have Handlebars not generate the newlines that the tags were occupying (without moving the tags themselves)? eg.

start
data
end

我之所以要这样做,是因为有些情况(如XML),换行符不合要求。

The reason why I want this is because there are cases (like in XML), where newlines are not desirable.

举例如下:

For example the following:

<parent>
    {{#each}}
        <child>{{.}}</child>
    {{/each}}
</parent>

会生成

<parent>

    <child>foo</child>

    <child>bar</child>

</parent>

将{{#each}},{{/ each}}折叠为一行会导致把手也可以在一条生产线上生成清单。例如:

Collapsing the {{#each}}, {{/each}} to a single line will cause Handlebars to generate lists on a single line as well. For example, this:

 <parent>
     {{#each}}<child>{{.}}</child>{{/each}}
 </parent>

会生成

 <parent>
     <child>foo</child><child>bar</child>    
 </parent>

所以为了生成没有额外换行符的XML,我的模板最终看起来像这样:

So in order to generate XML without extraneous newlines, my templates end up looking something like this:

 <parent>{{#each}}
     <child>{{.}}{{/each}}
 </parent>

谢谢!

Thanks!

推荐答案

请参阅这个问题。尝试在方括号中添加摇摆短划线,例如{{> partial〜}}而不是{{> partial}},这将删除换行符。在你的情况下,这将是:

See this question. Try adding swung dash to the brackets, e.g., {{> partial ~}} instead of {{> partial}}, this will remove the newline. In your case, it would be:

start
{{#if data ~}}
data
{{/if ~}}
end

start 
data 
end

这篇关于如何删除Handlebars生成的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆