【问题标题】:How to use Pug/Jade iterations to create multiple elements with different content?如何使用 Pug/Jade 迭代来创建具有不同内容的多个元素?
【发布时间】:2017-12-27 03:05:58
【问题描述】:

我目前正在学习 Pug/Jade 中的迭代。我想继续使用这个工具。

在下面的代码中,您基本上拥有相当于滑块的内容。

在第一个实例中,#{i} 应该为每个列出的类返回值 1-4,但编译时却是一样的。虽然我已经看到这种技术对其他人有用。

其次,关于我的data 数组,我已经能够让title 值显示在每个section 中,但是,让button 值出现在同一个容器中似乎成为挑战。

- var data=[{'title':'Home Run', 'button':'It\'s a win'}, {'title':'Testing', 'button':'Tested'}, {'title':'Foreground', 'button':'Background'}, {'title':'Forest', 'button':'Trees'}]


.slide-container
  - var i=0;
    while (i++ < 4)
      mixin list(title)
        section(class='slide-#{i}')
            h2= title


  each item in data
    +list(item.title)
    a(href='#')= item.button

上面的代码返回如下:

<div class="slide-container">
    <section class="slide-#{i}">
      <h2>Home Run</h2>
    </section><a href="#">It's a win</a>
    <section class="slide-#{i}">
      <h2>Testing</h2>
    </section><a href="#">Tested</a>
    <section class="slide-#{i}">
      <h2>Foreground</h2>
    </section><a href="#">Background</a>
    <section class="slide-#{i}">
      <h2>Forest</h2>
    </section><a href="#">Trees</a>
</div>

这很棒,但不是我需要的。我真正希望看到编译的是以下内容:

<div class="slide-container">
    <section class="slide-1">
      <h2>Home Run</h2>
      <a href="#">It's a win</a>
    </section>
    <section class="slide-2">
      <h2>Testing</h2>
      <a href="#">Tested</a>
    </section>
    <section class="slide-3">
      <h2>Foreground</h2>
      <a href="#">Background</a>
    </section>
    <section class="slide-4">
      <h2>Forest</h2>
      <a href="#">Trees</a>
    </section>
</div>

笔可以在这里查看:https://codepen.io/jobaelish/pen/jYyGQM?editors=1000

我必须如何处理我的代码才能获得所需的结果?


更新:

好的。所以,一个新的代码。我能够解决我最初遇到的一些问题,通过使用 + i 而不是 #{i} 来解决类迭代。

其次,通过在我的 Pug mixin 中添加 block 标签,我能够包含链接,没有问题。

这是新代码:

- var data=[{'title':'Home Run', 'button':'It\'s a win'}, {'title':'Testing', 'button':'Tested'}, {'title':'Foreground', 'button':'Background'}, {'title':'Forest', 'button':'Trees'}]


mixin list(title)
  h2= title
  block

.slide-container
  each item in data
    //- - var i = 0;
    //-   while i < 4
    section(class='slide-' + i++)
      +list(item.title)
        a(href='#')= item.button

呈现:

<div class="slide-container">
  <section class="slide-NaN">
    <h2>Home Run</h2><a href="#">It's a win</a>
  </section>
  <section class="slide-NaN">
    <h2>Testing</h2><a href="#">Tested</a>
  </section>
  <section class="slide-NaN">
    <h2>Foreground</h2><a href="#">Background</a>
  </section>
  <section class="slide-NaN">
    <h2>Forest</h2><a href="#">Trees</a>
  </section>
</div>

所以,剩下的唯一事情就是让类在编译时正确迭代。我得到了style-0style-5 和现在style-NaN 的一些结果。

我现在怎样才能让它以 style-1style-2 等的身份工作?

【问题讨论】:

    标签: pug pugjs jade4j


    【解决方案1】:

    回答你的第一个问题:属性插值(class='slide-#{i}' 等表达式)在 Pug 中是 not supported anymore。尝试改用class='slide-' + i

    关于第二个问题:你有两个独立的循环,这就是为什么按钮和标题是分开的。如果您希望它们出现在相同的 section 容器中,您需要以某种方式将两者放在一个循环中,以便一次迭代添加两者。

    关于你的第三个问题:我不完全确定我理解了这个问题,但这是一个pen,我将如何解决它:

    .slide-container
      each item, i in data
        section(class='slide-' + (i + 1))
          +list(item.title)
            a(href='#')= item.button
    

    【讨论】:

    • 第一部分我在这之后通过更多的研究发现了一点。第二部分我可以通过添加block 来解决。但是,我似乎无法在不重复数据变量的情况下重复 section。我将使用新代码更新问题。
    • 查看我编辑的问题以获取您上一个问题的答案。
    • 更新后的代码就像一个魅力!我无法告诉你我有多感激。我现在可以在我未来的代码中使用它......再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多