Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop in Swig template engine

I'm using Swig as a template engine for Express.js and I found no way to make a for loop with a variable like so:

for(var i=0; i<100; i++){
  //whatever
}

Is this even possible?

like image 677
Alvaro Avatar asked May 08 '26 06:05

Alvaro


1 Answers

As posted on your github issue for the same question, loops like this don't exist in Swig. You can iterate over actual objects and arrays, however. (See for-tag documentation).

Otherwise, you could create a range helper, as discussed here

swig.setDefaults({ locals: {
  range: function (start, len) {
    return (new Array(len)).join().split(',').map(function (n, idx) { return idx + start; });
  }
}});
like image 176
Paul Armstrong Avatar answered May 10 '26 14:05

Paul Armstrong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!