【发布时间】:2013-10-03 20:14:07
【问题描述】:
我正在尝试在 .each 循环中使用 HAML 生成以下代码。根据循环中的真/假条件,它应该使用div2 创建一个新的div1,或者在之前的div1 中附加div2。这是逻辑的简化:
Desired output:
if true, append `div2` within `div1`,
<div class="div1"> #added by true condition
div1 text
<div class="div2"> #added by true condition
div2 text
</div>
<div class="div2"> #added by false condition
div2 text
</div>
<div class="div2"> #added by false condition
div2 text
</div>
</div>
else, create a new `div1` and add `div2` within it
<div class="div1"> #added by true condition
div1 text
<div class="div2"> #added by true condition
div2 text
</div>
</div>
<div class="div1"> #new div1, added by true condition
div1 text
<div class="div2"> #added by true condition
div2 text
</div>
</div>
实际上,我正在尝试这样做
//within a loop logic
- if condition == true
.div1
div1 text
-# in all conditions
.div2
div2 text
这是我的 HAML 逻辑,我使用的是 tab_up,但我认为这不是正确的 HAML 方法:
//within a loop logic
- if condition == true
.div1
div1 text
- tab_up(2) #trying to indent the HAML, not just the HTML output
.div2
div2 text
【问题讨论】:
-
“以编程方式生成”是什么意思?为了使
div2在div1中,它需要在Haml 源代码中比div1缩进更多。tab_up只控制输出外观,不影响结构。您需要提供一个示例来说明您正在尝试做的事情。 -
问题不清楚。您排除的解决方案显然是正确的。
-
感谢 matt 和 depa,我已尝试进一步澄清这一点。
标签: ruby-on-rails-3 haml