最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - printing elements of array in Pug - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Oct 23, 2016 at 12:17 Andrea Sindico asked Oct 23, 2016 at 8:09 Andrea SindicoAndrea Sindico 7,4407 gold badges49 silver badges87 bronze badges 9
  • pugjs/language/iteration.html – hjpotter92 Commented Oct 23, 2016 at 8:39
  • What output are you getting? – hjpotter92 Commented Oct 23, 2016 at 8:42
  • see the update thank you – Andrea Sindico Commented Oct 23, 2016 at 12:18
  • It should be li= and not li = – hjpotter92 Commented Oct 23, 2016 at 12:20
  • wow I didn't get it is sensitive to white spaces. Post the answer – Andrea Sindico Commented Oct 23, 2016 at 19:30
 |  Show 4 more ments

1 Answer 1

Reset to default 5

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>
发布评论

评论列表(0)

  1. 暂无评论