Loop in Jade (currently known as the “Pug”)

I want to use a simple loop like for(int i=0; i<10; i++){} .

How to use it in a jade engine? I work with Node.js and use the expressjs framework.

+48
javascript for-loop pug express
Jan 04 2018-12-01T00:
source share
7 answers

eg:

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

you can see https://github.com/visionmedia/jade detailed document.

+79
Jan 04 2018-12-12T00:
source share

Using node I have @stuff and access it as follows:

 - each stuff in stuffs p = stuff.sentence 
+67
Jan 04 2018-12-14T00:
source share

An unusual but beautiful way to do it.

Without index :

 each _ in Array(5) = 'a' 

Will be printed: aaaaa

With an index :

 each _, i in Array(5) = i 

It will be printed: 01234

Notes . In the above examples, I assigned the jade each iteration syntax val parameter to _ because it is necessary, but will always return undefined .

+18
Apr 25 '16 at 21:53
source share

Here is a very simple jade file that has a loop in it. Jade is very space-sensitive. After the line for the definition of the loop ( for ), you must specify the indent (tab) for what you want to go inside the loop. 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] 
+14
Jan 04 '12 at 16:33
source share

Just add one more option, as this may help someone who is trying to iterate over an array AND maintain the count. For example, the code below goes through an array named items and displays only the first 3 elements. Note that each and if are native jade and do not need a hyphen.

 ul - var count = 0; each item in items if count < 3 li= item.name - count++; 
+8
Jul 23 '14 at 14:11
source share

You can also speed up the process with a while (see here: http://jsperf.com/javascript-while-vs-for-loops ). Also much more concise and legible IMHO:

 i = 10 while(i--) //- iterate here div= i 
+5
Nov 12
source share

Pug (renamed from "Jade") is a template engine for developing a complete web application stack. It provides neat and clean syntax for writing HTML and supports strict indentation in the form of spaces (for example, Python). It was implemented using the JavaScript API. The language basically supports two iterative constructs: each at the same time. "for" can be used instead of "everyone." Please refer to the language link here:

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

Here is one of my snippets: each / for iteration in pug_screenshot

0
Aug 08 '17 at 7:02
source share



All Articles