【问题标题】:How do I access part of a list in Jinja2如何访问 Jinja2 中的部分列表
【发布时间】:2011-05-03 00:16:00
【问题描述】:

我正在尝试使用 jinja2 模板语言返回我的帖子列表中的最后 n(比如 5)个帖子:

{% for recent in site.posts|reverse|slice(5) %}
    {% for post in recent %}
        <li> <a href="/{{ post.url }}">{{ post.title }}</a></li>
    {% endfor %}
{% endfor %}

不过,这将返回整个列表。如何去除第一个或最后 n 个元素?

【问题讨论】:

    标签: python templates jinja2


    【解决方案1】:

    我也有同样的问题。这是一个简单的答案。这将检索 site.posts 中的最后五个项目:

    {% for recent in site.posts[-5:] %}
        {% for post in recent %}
            <li> <a href="/{{ post.url }}">{{ post.title }}</a></li>
        {% endfor %}
    {% endfor %}
    

    【讨论】:

    • 这是建议中最简单最优雅的答案。
    【解决方案2】:

    我认为不使用 slice 过滤器会更简单:

    {% for post in site.posts | reverse | list[0:4] %}
      <li>&raquo; <a href="/{{ post.url }}">{{ post.title }}</a></li>
    {% endfor %}
    

    另一种方法是使用loop controls extension:

    {% for post in site.posts | reverse %}
      {%- if loop.index > 4 %}{% break %}{% endif %}
      <li>&raquo; <a href="/{{ post.url }}">{{ post.title }}</a></li>
    {%- endfor %}
    

    【讨论】:

      【解决方案3】:

      我想出了以下代码:

      {% for x in xs | batch(n) | first %}
          ...
      {% endfor %}
      

      batch(n) 过滤器将列表xs 拆分为长度为n 的子列表,然后first 过滤器选择这些子列表中的第一个。

      【讨论】:

      • 我认为这应该是公认的答案。请注意,如果要使用last 而不是first,则必须首先将batch 的输出通过list 过滤器。
      【解决方案4】:

      尝试下标符号,就像在普通 Python 中一样。例如,获取最后 5 个帖子并以相反的顺序显示它们:

      import jinja2
      tmpl = """\
      {%- for col in posts[-5:]|reverse|slice(3) -%}
          {%- for post in col -%}
              {{ post }}
          {%- endfor -%}
          <br>
      {%- endfor -%}"""
      jinja2.Template(tmpl).render(posts=[1,2,3,4,5,6,7])
      

      产生:u'76&lt;br&gt;54&lt;br&gt;3&lt;br&gt;'

      【讨论】:

        【解决方案5】:

        对我来说,下面的简单代码可以工作,并且不需要整个 jinja 过滤器链。只需使用列表过滤器转换为列表,然后进行正常的数组切片(注意括号):

        {% for recent in (site.posts | list)[-5:] %}
          {% for post in recent %}
            <li> <a href="/{{ post.url }}">{{ post.title }}</a></li>
          {% endfor %}
        {% endfor %}
        

        我遇到了同样的问题,但是我的数据是一个序列而不是一个列表,而这段代码同时处理了这两个问题。

        【讨论】:

          【解决方案6】:

          @Andrey 的回答是正确的。但是,要完全解决您的问题:

          {% for recent in site.posts|batch(5)|list|last|reverse %}
                  <li> <a href="/{{ recent.url }}">{{ recent.title }}</a></li>
          {% endfor %}
          

          或者:

          {% for recent in site.posts|reverse|batch(5)|first %}
                  <li> <a href="/{{ recent.url }}">{{ recent.title }}</a></li>
          {% endfor %}
          

          您使用哪一种取决于您的喜好。

          【讨论】:

            【解决方案7】:
            {% for recent in site.posts[-5:][::-1] %}
                {% for post in recent %}
                    <li> <a href="/{{ post['url'] }}">{{ post['title'] }}</a></li>
                {% endfor %}
            {% endfor %}
            
            1. [-5:] - 最后 5 个帖子
            2. [::-1] - 逆序

            【讨论】:

              【解决方案8】:

              要获取最后一个元素,请从数组列表中获取总索引。

              例如,您的对象名称是foundappointmentlog

              {% set total=foundappointmentlog|length %} //it return length
              {{foundappointmentlog[total-1].appointment_result}}  // here you get your last value using index 
              

              【讨论】:

              • 这没有回答问题,因为 OP 不询问最后一个元素,而是询问第一个/最后一个 n 元素。
              猜你喜欢
              • 1970-01-01
              • 2020-12-30
              • 1970-01-01
              • 2017-07-24
              • 2013-12-18
              • 1970-01-01
              • 1970-01-01
              • 2019-01-29
              • 2016-02-17
              相关资源
              最近更新 更多