本文介绍了else 块中的缩进是否从 then 块继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下 Pug 脚本:
- data = [ "A", "B", "C", "D" ]- for (i=0,i
产生:
<div class="col">A</div>
<div class="col">B</div><div class="row"><div class="col">C</div>
<div class="col">D</div>
我想要的是:
<div class="col">A</div><div class="col">B</div>
<div class="row"><div class="col">C</div><div class="col">D</div>
为什么 else 块中的缩进没有遵循?如何实现我想要的输出?
解决方案
Pug 本质上不允许在 else
块中使用这种额外"的缩进.为了达到你想要的结果,你可以这样想——
- var data = [ "A", "B", "C", "D" ]每个数据,数据中的索引如果 ((索引 % 2) == 0).排.col #{数据}如果(数据[索引+ 1]).col #{数据[索引 + 1]}
——产生——
<div class="col">A</div><div class="col">B</div>
<div class="row"><div class="col">C</div><div class="col">D</div>
The following Pug script:
- data = [ "A", "B", "C", "D" ]
- for (i=0,i<data.length;i++)
- var even = (i%2)==0
if even
.row
.col #{data[i]}
else
.col #{data[i]}
produces:
<div class="row">
<div class="col">A</div>
</div>
<div class="col">B</div>
<div class="row">
<div class="col">C</div>
</div>
<div class="col">D</div>
What I want is:
<div class="row">
<div class="col">A</div>
<div class="col">B</div>
</div>
<div class="row">
<div class="col">C</div>
<div class="col">D</div>
</div>
Why is the indent in the else block not followed? How do I achieve my desired output?
解决方案
Pug, by nature, doesn't allow that kind of "extra" indentation within the else
block. To achieve your desired result, you could think about it like this—
- var data = [ "A", "B", "C", "D" ]
each datum, index in data
if ((index % 2) == 0)
.row
.col #{datum}
if (data[index + 1])
.col #{data[index + 1]}
—which yields—
<div class="row">
<div class="col">A</div>
<div class="col">B</div>
</div>
<div class="row">
<div class="col">C</div>
<div class="col">D</div>
</div>
这篇关于else 块中的缩进是否从 then 块继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!