theflametrooper
theflametrooper

Reputation: 48

How do I iterate over multiple objects in jade

Here is my code:

extends ../layout
block content
  -
    projects = [ { "name": "testProject", "status": "Active", "github": "", "slack": "", "trello", "" }, { "name": "testProject", "status": "Active", "github": "", "slack": "", "trello", "" } ]

  div.container
    // Nav tabs
    ul.nav.nav-tabs(role='tablist')
      li.active(role='presentation')
        a(href='#active', aria-controls='active', role='tab', data-toggle='tab') Active
      li(role='presentation')
        a(href='#inactive', aria-controls='inactive', role='tab', data-toggle='tab') Inactive
    // Tab panes
    .tab-content
      #active.tab-pane.active(role='tabpanel')
        each project in projects
          #projects
            .media
              .media-left
                i.fa.fa-2x.fa-code.media-object
              .media-body
                h4.media-heading #{project.name}
                span.label.label-success #{project.status}
                span.label.label-warning Help Wanted
                .links
                  span.github
                    i.fa.fa-code-fork
                    a(href='#{project.github}') GitHub
                  span.slack
                    i.fa.fa-slack
                    a(href='#{project.slack}') Slack
                  span.trello
                    i.fa.fa-trello
                a(href='#{project.trello}') Trello

I need to iterate over every object in the array and display each one using jade. I'm not sure what to do! I followed these tutorials:

http://jade-lang.com/reference/code/ iterate over an array of objects in jade/pugjs

Upvotes: 0

Views: 407

Answers (2)

Tim
Tim

Reputation: 1826

Your JSON objects seem to be a little off... try this

Your Json looks like this "trello", "" change it into this "trello": "" This will validate your json object so it will work in the jade each loop.

Below is an example:

projects = [ 
{
  "name": "testProject",
  "status": "Active",
  "github": "",
  "slack": "",
  "trello": ""
}, 
{
  "name": "testProject",
  "status": "Active",
  "github": "",
  "slack": "",
  "trello": ""
} ]

Just copy and past the below into your file and it should work!

 projects = [ { "name": "testProject", "status": "Active", "github": "", "slack": "", "trello": "" }, { "name": "testProject", "status": "Active", "github": "", "slack": "", "trello": "" } ]

Upvotes: 2

t1m0n
t1m0n

Reputation: 3431

Use = sign to access properties:

-var projects = [{name: 'name1', link: "link1"}, {name: 'name2', link: 'link2'}]

each project in projects
  div
     a(href=project.link)= project.name

If you want to insert variable into string, use #{var} notation:

a(href='my-link-#{project.link}')= project.name

Upvotes: 0

Related Questions