Loop through object properties nunjucks

前端 未结 2 1383
广开言路
广开言路 2021-02-13 12:40

I\'ve got following model:

items: {
    someId1: 
        {
            property1....
        },
    someId2: {...},
    someIdN: {...}
}

I wou

相关标签:
2条回答
  • 2021-02-13 13:10

    You can use nested loops like this:

    <ul>
      {% for item in items %}
        {% for something in item.someId1 %}
          <li>
            {{ something.property1 }}
          </li>
        {% endfor %}
      {% endfor %}
    </ul>
    

    For this json object:

    items: {
      someId1: {
        property1: "It makes you want to shout! Raise your hands up and..."
      },
      someId2: {...},
      someIdN: {...}
    }
    
    0 讨论(0)
  • 2021-02-13 13:22

    This answer is actually right on the Nunjucks homepage:

    <ul>
       {% for name, item in items %}
          <li>{{ name }}: {{ item }}</li>
       {% endfor %}
    </ul>
    

    In your case this would be:

    <ul>
       {% for someId, item in items %}
          <li>{{ someId }}: {{ item.property1 }}</li>
       {% endfor %}
    </ul>
    

    As you can use the for loop for arrays and object/hashes.

    0 讨论(0)
提交回复
热议问题