如何在swig模板中从JSON中获取n元素

时间:2014-12-08 14:46:05

标签: json swig-template

我是新手......

我有一些JSON

{
    "article": [
        {
            "id": "21",
            "type": "news",
            "title": "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
            "summary": "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
            "url": "http://www....",
            "website": "www...",
            "authors": [],
            "images": [
                {
                    "title": "",
                    "dateCreated": "",
                    "name": "",
                    "path": ""
                }
            ]
        },
        {
            "id": "22",
            "type": "news",
            "title": "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
            "summary": "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
            "url": "http://www....",
            "website": "www...",
            "authors": [],
            "images": [
                {
                    "title": "",
                    "dateCreated": "",
                    "name": "",
                    "path": ""
                }
            ]
        },
        {
            "id": "23",
            "type": "news",
            "title": "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
            "summary": "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
            "url": "http://www....",
            "website": "www...",
            "authors": [],
            "images": [
                {
                    "title": "",
                    "dateCreated": "",
                    "name": "",
                    "path": ""
                }
            ]
        }
    ]
}

这个JSON有3个以上的元素。 我不知道如何以2,3,1,4,5,6,7,8,......的顺序模板。

如何循环一次模板whith json,并将属性值放在我的html标签中?

我想要

<ul>
  <li>
    <img scr="path to object 2">
    <a href="path to object 2">title of object 2</a>
  </li>
  <li>
    <img scr="path to object 3">
    <a href="path to object 3">title of object 3</a>
  </li>
  <li>
    <img scr="path to object 1">
    <a href="path to object 1">title of object 1</a>
  </li>
  <li>
    <img scr="path to object 4">
    <a href="path to object 4">title of object 4</a>
  </li>
  <li>
    <img scr="path to object 5">
    <a href="path to object 5">title of object 5</a>
  </li>
  ...
  <li>
    <img scr="path to object n">
    <a href="path to object n">title of object n</a>
  </li>
</ul>

任何消化?

1 个答案:

答案 0 :(得分:0)

{% for article in articles %}
  {% if loop.index == 2 %}
    <li>
      <img src="{{ article.images[0].path }}" />
      <a href="{{ article.url }}">{{ article.title }}</a>
    </li>
  {% endif %}
{% endfor %}
{% for article in articles %}
  {% if loop.index == 3 %}
    <li>
      <img src="{{ article.images[0].path }}" />
      <a href="{{ article.url }}">{{ article.title }}</a>
    </li>
  {% endif %}
{% endfor %}
{% for article in articles %}
  {% if loop.index == 1 %}
    <li>
      <img src="{{ article.images[0].path }}" />
      <a href="{{ article.url }}">{{ article.title }}</a>
    </li>
  {% endif %}
{% endfor %}

依旧......