Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
How do I iterate through an inline var using pug

I am attempting to iterate through an array and each time add 1 to the inline pug variable. So far this is what I got.

ul(class="benchcards")
            - var i = -1
            each card in cards
                -var L = i + 1
                li
                    a.btn(href="/addmonster" + L)
                        div=card.name
                        div= "Attack: "+card.attack
                        div= "HP: "+card.hp
                        div= "Attribute: "+card.attribute
                        div= "Energy: "+card.energy

When I run the page "L" is always = to 0. I want it to add 1 each time. The question really comes down to what am I doing wrong?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

As said in the comments you are not incrementing the i, you can fix your issue like this:

ul.benchcards
  - var i = -1;
  each card in cards
    li
      a.btn(href='/addmonster' + i++)
        div= card.name
        div= 'Attack: ' + card.attack
        div= 'HP: ' + card.hp
        div= 'Attribute: ' + card.attribute
        div= 'Energy: ' + card.energy
about 4 years ago · Juan Pablo Isaza Report

0

It's better to use the index built into the each iterator in Pug. That way you don't have to declare or increment a variable.

ul.benchcards
  each card, i in cards
    li
      a.btn(href=`/addmonster${i}`)
        div #{card.name}
        div Attack: #{card.attack}
        div HP: #{card.hp}
        div Attribute: #{card.attribute}
        div Energy: #{card.energy}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!