简体   繁体   English

循环在Jade(目前称为“Pug”)模板引擎

[英]Loop in Jade (currently known as “Pug”) template engine

I want to use a simple loop like for(int i=0; i<10; i++){} . 我想使用像for(int i=0; i<10; i++){}这样的简单循环。

How do I use it in the Jade engine? 我如何在Jade引擎中使用它? I'm working with Node.js and use the expressjs framework. 我正在使用Node.js并使用expressjs框架。

for example: 例如:

- for (var i = 0; i < 10; ++i) {
  li= array[i]
- }

you may see https://github.com/visionmedia/jade for detailed document. 您可以查看https://github.com/visionmedia/jade获取详细文档。

Using node I have a collection of stuff @stuff and access it like this: 使用节点我有一些东西@stuff的集合,并像这样访问它:

- each stuff in stuffs
  p
    = stuff.sentence

An unusual but pretty way of doing it 这是一种不寻常但非常漂亮的方式

Without index : 没有索引

each _ in Array(5)
  = 'a'

Will print: aaaaa 将打印: aaaaa

With index : 有索引

each _, i in Array(5)
  = i

Will print: 01234 将打印: 01234

Notes : In the examples above, I have assigned the val parameter of jade's each iteration syntax to _ because it is required, but will always return undefined . 注意 :在上面的例子中,我已经将jade的each迭代语法的val参数赋给_因为它是必需的,但是总是返回undefined

Here is a very simple jade file that have a loop in it. 这是一个非常简单的jade文件,里面有一个循环。 Jade is very sensitive about white space. 玉对白色空间非常敏感。 After loop definition line ( for ) you should give an indent(tab) to stuff that want to go inside the loop. 在循环定义行( for )之后,你应该给想要进入循环的东西一个缩进(制表符)。 You can do this without {} : 你可以不用{}来做到这一点:

- var arr=['one', 'two', 'three'];
- var s = 'string';
doctype html
html
    head
    body
        section= s
        - for (var i=0; i<3; i++)
            div= arr[i]

Just adding another possibility as it might help someone that's trying to both iterate over an array AND maintain a count. 只是添加另一种可能性,因为它可能会帮助那些尝试迭代数组并保持计数的人。 For example, the code below goes through an array named items and only displays the first 3 items. 例如,下面的代码通过一个名为items的数组,只显示前3个项目。 Notice that the each and the if are native jade and don't need a hyphen. 请注意, eachif都是原生玉,不需要连字符。

ul
  - var count = 0;
  each item in items
    if count < 3
      li= item.name
    - count++;

You could also speed things up with a while loop (see here: http://jsperf.com/javascript-while-vs-for-loops ). 您还可以使用while循环加速(请参阅此处: http//jsperf.com/javascript-while-vs-for-loops )。 Also much more terse and legible IMHO: 还有更多简洁易读的恕我直言:

i = 10
while(i--)
    //- iterate here
    div= i

Pug (renamed from 'Jade') is a templating engine for full stack web app development. Pug(从'Jade'重命名)是用于完整堆栈Web应用程序开发的模板引擎。 It provides a neat and clean syntax for writing HTML and maintains strict whitespace indentation (like Python). 它为编写HTML提供了一个干净整洁的语法,并维护严格的空白缩进(如Python)。 It has been implemented with JavaScript APIs. 它已使用JavaScript API实现。 The language mainly supports two iteration constructs: each and while. 该语言主要支持两种迭代结构:each和while。 'for' can be used instead 'each'. 'for'可以代替'each'使用。 Kindly consult the language reference here: 请在此查阅语言参考:

https://pugjs.org/language/iteration.html https://pugjs.org/language/iteration.html

Here is one of my snippets: each/for iteration in pug_screenshot 这是我的一个片段: 每个/用于pug_screenshot中的迭代

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM