Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在Jade中动态创建行?_Node.js_Pug - Fatal编程技术网

Node.js 如何在Jade中动态创建行?

Node.js 如何在Jade中动态创建行?,node.js,pug,Node.js,Pug,现在我有这个: - count = 0 - each organization in organizations if (count == 0 || count % 3 == 0) .row .col-md-4 a(href="#{organization.url}" target="_blank") .fancyy-box h3= organization.name

现在我有这个:

- count = 0
- each organization in organizations
    if (count == 0 || count % 3 == 0)
        .row
    .col-md-4
        a(href="#{organization.url}" target="_blank")
            .fancyy-box
                h3= organization.name
                img(src="/images/organizations/#{organization.logo}")
                p= organization.mission
- count = count + 1
我想做的是每三个组织开始新的一行,这样我就可以:

<div class="row">
    <div class="organization">...</div>
    <div class="organization">...</div>
    <div class="organization">...</div>
</div>
<div class="row">
    <div class="organization">...</div>
    <div class="organization">...</div>
    <div class="organization">...</div>
</div>
// and so on...

...
...
...
...
...
...
//等等。。。
现在我明白了:

<div class="row" />
<div class="organization">...</div>
<div class="organization">...</div>
<div class="organization">...</div>
<div class="row" />
<div class="organization">...</div>
<div class="organization">...</div>
<div class="organization">...</div>
<div class="row" />

...
...
...
...
...
...
有没有一个简单的方法来实现这一点?

就像你说的,你需要考虑你的缩进。但这只是问题的一部分,我认识到的第二件事是你的条件语句和循环。我对其进行了编辑,如下所示:

- for (var j = 0; j < organizations.length; j++)
  if (j == 0 || j % 3 == 0)
    .row
      - for (var i = j; i < (3 + j); i++ )
        - if (i >= organizations.length) break;
          .col-md-4
            a(href="#{organizations[i].url}" target="_blank")
              .fancyy-box
                h3= organizations[i].name
                img(src="/images/organizations/#{organizations[i].logo}")
                p= organizations[i].mission
现在,HTML输出:

<div class="row">
  <div class="col-md-4">
    <a href="example.com" target="_blank">
      <div class="fancyy-box">
        <h3>demo1</h3><img src="/images/organizations/pic1.jpg"/>
        <p>1</p>
      </div>
    </a>
  </div>
  <div class="col-md-4">
    <a href="anotherexample.com" target="_blank">
      <div class="fancyy-box">
        <h3>demo2</h3><img src="/images/organizations/pic2.jpg"/>
        <p>2</p>
      </div>
    </a>
  </div>
  <div class="col-md-4">
    <a href="justanotherexample.com" target="_blank">
      <div class="fancyy-box">
        <h3>demo3</h3><img src="/images/organizations/pic3.jpg"/>
        <p>3</p>
      </div>
    </a>
  </div>
</div>
<div class="row">
  <div class="col-md-4">
    <a href="wowjustanotherexample.com" target="_blank">
      <div class="fancyy-box">
        <h3>demo4</h3><img src="/images/organizations/pic4.jpg"/>
        <p>4</p>
      </div>
    </a>
  </div>
</div>

您可以这样做

 .row 
   each organization, i in organizations
     if i > 0 && i % 3 === 0
       // jade/pug can interpret html
       </div><div class="row">
       // past you col template here
       .col-md-4
        a(href="#{organizations[i].url}" target="_blank")
          .fancyy-box
        ...
.row
每个组织,我在组织中
如果i>0&&i%3==0
//jade/pug可以解释html
//你过去了吗
.col-md-4
a(href=“#{organizations[i].url}”target=“_blank”)
.fancyy盒子
...

jade通过缩进工作。在.row-one选项卡之后移动所有内容。@MukeshSoni,该选项卡将在
的if语句中包含
组织
输出。这确实得到了回答,ztirom提供的解决方案正是我想要的。编辑我的答案需要更长的时间,第一次编辑并不是问题作者想要的。很抱歉,修复这个问题花费了超过一秒钟的时间。我还没有真正尝试过这个方法,但看了之后,它看起来应该像冠军一样工作。非常感谢!这个答案不是最优雅的IMO——它使用了一些复杂的for循环,而答案中的实际json数据是无效的json。这里的迭代有点笨重。我在这里用答案解决了同样的问题:
 .row 
   each organization, i in organizations
     if i > 0 && i % 3 === 0
       // jade/pug can interpret html
       </div><div class="row">
       // past you col template here
       .col-md-4
        a(href="#{organizations[i].url}" target="_blank")
          .fancyy-box
        ...