简体   繁体   English

Handlebars.net 不保留空白

[英]Handlebars.net not preserving white space

I have the following template:我有以下模板:

<!DOCTYPE html>
<html>
<body>
    {{>Header}}
    This is a test template.
    {{>Footer}}
</body>
</html>

When I compile this template, I'd expect to get this:当我编译这个模板时,我希望得到这个:

<!DOCTYPE html>
<html>
<body>
    This is a header.
    This is a test template.
    This is a footer.
</body>
</html>

Instead, what I get is this:相反,我得到的是:

<!DOCTYPE html>
<html>
<body>
This is a header.    This is a test template.
This is a footer.</body>
</html>

The indentations before the first and third lines are gone;第一行和第三行前的缩进没了; and the newlines before the second line and closing body tag are gone.并且第二行和结束正文标记之前的换行符消失了。 Is this expected, and is there a way to preserve the whitespace just as it is laid out in the base template?这是预期的,有没有办法像在基本模板中一样保留空白? I should note that I'm using Handlebars.Net here, although my understanding is that it's meant to emulate the original Javascript spec as closely as possible.我应该注意,我在这里使用的是Handlebars.Net ,尽管我的理解是它旨在尽可能地模拟原始 Javascript 规范。

(Answer from the Github issue where this was also posted) : (来自Github 问题的答案,该问题也已发布)

So there's two different things going on here that I'll summarize first and then explain: 1) what you expect is incorrect;所以这里有两件不同的事情,我将首先总结然后解释:1)你的期望是不正确的; 2) what you are getting is also incorrect, in a different way: 2)你得到的也是不正确的,以不同的方式:

  1. Whitespace (and line breaks) are not significant in HTML, and though Handlebars is technically a general string templating language, the design decisions & opinions it contains are heavily slanted towards using it for an HTML templating language.空格(和换行符)在 HTML 中并不重要,虽然 Handlebars 在技术上是一种通用的字符串模板语言,但它包含的设计决策和意见非常倾向于将它用于 HTML 模板语言。 You should not expect it to preserve implicit line breaks, only explicit line breaks (eg if you put \\n it will preserve that)您不应该期望它保留隐式换行符,只保留显式换行符(例如,如果您输入 \\n 它将保留该换行符)

  2. Handlebars.Net actually DOES preserve some line breaks when it's not supposed to! Handlebars.Net 实际上确实保留了一些不应该保留的换行符! That's a bug but one many users are currently relying on, so we'll keep it in 1.x but fix it in v2.这是一个错误,但许多用户目前都依赖它,因此我们将其保留在 1.x 中,但在 v2 中修复它。

To get your desired output, put explicit line breaks in your template.要获得所需的输出,请在模板中放置明确的换行符。 Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM