Andrea Sindico
Andrea Sindico

Reputation: 7440

printing elements of array in Pug

I am new to Jade/Pug template engine used in Express I need to print out the name property of a list objects contained in an associative array passed as parameter to the pug template from an express route module.

I am trying in different ways like

  each element in listOfElements
      p #{element.name}

where listOfElements is the name of the passed parameter

But I can't get the desired result

UPDATE

I am now trying to follow the doc which only provides a UL example (not what I need).

According to the doc I am going like this

ul
each element in listOfElements
    li = element.name

What I get on the rendered page is a list in which each bullet contains the " = element.name" text

Upvotes: 3

Views: 10203

Answers (1)

hjpotter92
hjpotter92

Reputation: 80639

Going by the documentation on Pug website regarding iterations, you can get buffered code by placing an = right after the tag name. The docs for the same lie here. Therefore, in your second attempt, the following will work:

ul
each element in listOfElements
    li= element.name

However, as to the first attempt, I tried the following code, and it gave me the expected output (as shown after the snippet):

ul
  each val in [{1:'a'}, {1:2}, {1:3}, {1:4}, {1:5}]
    li #{val[1]}

outputs:

<ul>
  <li>a</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

Upvotes: 5

Related Questions