【问题标题】:Why won't Jade indent the code?为什么 Jade 不缩进代码?
【发布时间】:2012-05-16 13:40:07
【问题描述】:

我无法让 Jade 正确缩进 for 循环中的代码:

div.container
    - var cell = 0
    - for(var i = 0; i < results.Items.Item.length; i++)
        - if(cell == 0)
            div.row
                div.span3
                    div.cell
                        span #{results.Items.Item[i].ItemAttributes.Title}
        - else
                    div.span3
                        div.cell
                            span #{results.Items.Item[i].ItemAttributes.Title}
        - if(cell < 3)
            - cell++;
        - else
            - cell = 0

else 语句中的代码不会进入 div.row 内部,而是呈现在 div.container 下,或者与 div.row 处于同一级别。我需要 else 语句中的代码在 div.row 中呈现。我怎样才能让 Jade 这样做?

我要做的是让 div.row 进行渲染,然后在每 4 个 div.cell(由单元格对象计数)之后渲染另一个 div.row

【问题讨论】:

    标签: node.js express pug


    【解决方案1】:

    我相信你必须这样做:

    div.container
        - var cell = 0
        - for(var i = 0; i < results.Items.Item.length; i++)
            div.row
                div.span3
                    div.cell
                        span #{results.Items.Item[i].ItemAttributes.Title}
    
            - if(cell < 3)
                - cell++;
            - else
                div.row
                - cell = 0
    

    【讨论】:

    • 我不遵循...总是呈现 div.row - 目标是仅在满足条件时呈现 div.row。我需要 div.cell 在 div.row 下缩进,但我不希望每次迭代都使用 div.row。
    • 等等,您是说需要 Jade 在渲染后以特定的自定义缩进输出?我相信这是不可能的。
    • 这不是真正的自定义 - 你不能在 else 语句中缩进是没有任何意义的,因为 else 语句在遇到与 else 语句相同级别的其他内容之前永远不会结束。那么如果 else 语句永远不会结束,你如何缩进呢?
    • 抱歉,我误解了你的意图。检查我的编辑,这是否符合您的要求?
    【解决方案2】:

    使用大括号“{”和“}”

    div.container
            - var cell = 0
            - for(var i = 0; i < results.Items.Item.length; i++){
                - if(cell == 0){
                    div.row
                        div.span3
                            div.cell
                                span #{results.Items.Item[i].ItemAttributes.Title}
                - }
                - else{
                            div.span3
                                div.cell
                                    span #{results.Items.Item[i].ItemAttributes.Title}
                - }
                - if(cell < 3)
                    - cell++
                - else
                    - cell = 0
            - }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多