玉:if语句和嵌套

时间:2013-01-19 14:40:26

标签: pug

Concider这个伪服务器端代码

if(isFixed) {
  <div class="fixed">
} else {
  <div>
}
    <p>Inner element</p>
  </div>

我试着用玉石做这件事,但是......

 - if(mode === 'fixed') {
   div#tabbar
 - }
     p ...I cannot get this to be an inner element :(

</div>已关闭的情况下,它总是这样呈现:

<div id="tabbar"></div><p>I want this inside of the div</p>

我弄乱了这个凹痕吗? 谢谢!

3 个答案:

答案 0 :(得分:19)

您需要将控制流与模板分开。试试这个:

divClass = null

if isFixed
   divClass = "fixed"

div(class=divClass)
   p inner element

反过来可能会建议将&#34; isFixed&#34;参数进入要传递的实际divClass参数。但这当然取决于你剩下的代码/模板。


作为替代方案,请尝试使用mixin:

mixin content
  p inner element

if (isFixed)
  div(class="test")
    mixin content
else
  div(class="other")
    mixin content

答案 1 :(得分:9)

我会用三元(或完全写出的条件或方法)来确定类属性。这允许你将div保持在一行并保持缩进,就像对任何其他元素一样:

div(class="#{ isFixed ? 'fixed' : '' }")
    p Inner Element

答案 2 :(得分:2)

Jade似乎没有内置的解决方案来启动和结束标签,而不是使用“管道”#39;用于呈现纯文本的字符。

- if(mode === 'fixed') {
| <div id="tabbar">
- }
| <p>I cannot get this to be an inner element :(</p>
- if(mode === 'fixed') {
| </div>
- }

不那么混乱的解决方案 -

div(class=(isFixed) ? 'fixed' : '')
  p inner element
相关问题