[英]How to render a variable in HAML and preserve white-spaces?
This is what I'm doing: 这就是我在做什么:
%pre
=var
The value of var
is "foo\\nbar"
. var
值为"foo\\nbar"
。 I'm getting this HTML: 我得到这个HTML:
<body>
<pre>foo
bar</pre>
</body>
Obviously, I'm expecting: 显然,我期望:
<body>
<pre>foo
bar</pre>
</body>
What to do? 该怎么办?
You have to place your var
on the same line where your %pre
is. 您必须将var
放在%pre
所在的同一行。 So it looks like %pre= var
所以看起来像%pre= var
If you use ugly mode then you shouldn't have this problem. 如果您使用丑陋的模式,那么您应该不会遇到这个问题。 Note that in (the hopefully coming soon) Haml 5 there only is ugly mode , so this is probably the best option for the future. 请注意,在Haml 5中(希望很快就会到来) 只有丑陋的模式 ,因此这可能是将来的最佳选择。
This would produce: 这将产生:
<body>
<pre>foo
bar</pre>
</body>
For current versions, in non-ugly mode, if you wanted to preserve whitespace like this then normally you would use ~
, which would call find_and_preserve
on the result. 对于当前版本,在非丑陋模式下,如果要保留这样的空格,则通常使用~
,它将在结果上调用find_and_preserve
。 However here you are already in a pre
block, and so that won't work (arguably this is a bug, but with the removal of non-ugly mode in Haml 5 I can't see any benefit of fixing it). 但是,这里您已经处于pre
,因此将无法正常工作(可以说这是一个错误,但是随着Haml 5中非丑陋模式的删除,我看不到对其进行修复的任何好处)。
You could use preserve
directly though: 您可以直接使用preserve
:
%body
%pre
=preserve(var)
This produces: 这将产生:
<body>
<pre>foo
bar</pre>
</body>
which has the same affect as your desired result when rendered. 呈现效果与您期望的结果相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.