【问题标题】:How can I display certain div only for the first 2 iterations of a jade each loop?如何仅在每个循环的前 2 次迭代中显示某些 div?
【发布时间】:2013-12-11 08:21:25
【问题描述】:

我只想为每个循环的翡翠的前 2 次迭代打印一个 div。我怎样才能在翡翠中做类似的事情

counter = 0

for each tab in tabs
  if (counter == 0)
    div.nav-header NAVIGATION
    counter++
  else if (counter == 1)
    div.nav-header MY GROUPS
    counter++

【问题讨论】:

    标签: javascript node.js for-loop pug


    【解决方案1】:

    看看case 块:http://jade-lang.com/reference/

    - var counter = 0
    each tab in tabs
      case counter
        when 0
          div.nav-header NAVIGATION
        when 1
          div.nav-header MY GROUPS
      - counter++
    

    当然tabs 应该是您传递给 Jade 引擎的数组变量。

    更简洁的选择可以只使用数组索引:

    each tab, counter in tabs
      case counter
        when 0
          div.nav-header NAVIGATION
        when 1
          div.nav-header MY GROUPS
    

    或者使用if 块甚至更短(但我更喜欢case 块以提高可读性

    each tab, counter in tabs
      if counter === 0
        div.nav-header NAVIGATION
      if counter === 1
        div.nav-header MY GROUPS
    

    【讨论】:

      猜你喜欢
      • 2020-07-20
      • 2014-06-18
      • 2017-04-28
      • 2019-09-22
      • 2014-03-23
      • 2016-11-08
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      相关资源
      最近更新 更多