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

88
Views
How to make a custom class iterable in Pug?

So, I have this custom paginator class, and in it I have implemented the methods [Symbol.iterator] and forEach:

class MyPaginator<T>{

  items: T[]

  //...

  [Symbol.iterator](){
    return this.items[Symbol.iterator]();
  };

  forEach(callback: (item: T, index: number) => void): void {
    return this.items.forEach(callback);
  }

  //...
}

With this, I can iterate through it in "normal" code with

for(let element of paginator) {}
// or
paginator.forEach(elements => {})

But apparently pug does not use any of those for it's each iterator, because this does not work:

ul
  each element in paginator
    li
      // something

Although this works:

ul
  each element in paginator.items
    li
      // something

Is there anything I can implement in my class so that I'm able to iterate through it in Pug without needing the .items?

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

0

You can use each-of with Symbol.iterator if you are using pug >= 3.

ul
  each item of paginator
    li= item
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!