如何访问 Jinja2 中列表的一部分

发布于 2024-09-29 13:59:26 字数 314 浏览 6 评论 0原文

我正在尝试使用 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 个或后 n 个元素?

I'm trying to use the jinja2 templating langauge to return the last n(say, 5) posts in my posts list:

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

This is returning the whole list though. How do you strip the first or last n elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

瑶笙 2024-10-06 13:59:26

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

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

I had the same problem too. It's a simple answer. This retrieves the last five items in site.posts:

{% for recent in site.posts[-5:] %}
    {% for post in recent %}
        <li> <a href="/{{ post.url }}">{{ post.title }}</a></li>
    {% endfor %}
{% endfor %}
怀中猫帐中妖 2024-10-06 13:59:26

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

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

另一种方法是使用 循环控制扩展

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

this is a bit simpler I think without the use of the slice filter:

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

another way is to use the loop controls extension:

{% for post in site.posts | reverse %}
  {%- if loop.index > 4 %}{% break %}{% endif %}
  <li>» <a href="/{{ post.url }}">{{ post.title }}</a></li>
{%- endfor %}
静若繁花 2024-10-06 13:59:26

我想出了以下代码:

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

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

I came up with the following code:

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

The batch(n) filter splits a list xs into sublists of length n, then the first filter selects the first of these sublists.

不必你懂 2024-10-06 13:59:26

尝试使用下标表示法,就像在普通 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
54
3
'

Try subscript notation, as in normal Python. For example, to take the last 5 posts and display them in reverse order:

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])

produces: u'76<br>54<br>3<br>'

别理我 2024-10-06 13:59:26

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

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

我遇到了同样的问题,但我的数据是序列而不是列表,并且此代码可以处理两者。

For me, the following simple code works and doesn't require the whole chain of jinja filters. Simply use the list filter to convert to list and then do normal array slicing (note the parantheses):

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

I had the same problem, but my data was in a sequence rather than a list and this code handles both.

善良天后 2024-10-06 13:59:26

@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 %}

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

@Andrey's answer has the right idea. However, to fully solve your question:

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

Alternatively:

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

Whichever one you use depends on your preferences.

靑春怀旧 2024-10-06 13:59:26
{% 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] - 倒序
{% for recent in site.posts[-5:][::-1] %}
    {% for post in recent %}
        <li> <a href="/{{ post['url'] }}">{{ post['title'] }}</a></li>
    {% endfor %}
{% endfor %}
  1. [-5:] - last 5 posts
  2. [::-1] - reverse order
鲜血染红嫁衣 2024-10-06 13:59:26

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

例如,您的对象名称是 foundappointmentlog

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

To get the last element, get total index from the array list.

For example, your object name is foundappointmentlog.

{% set total=foundappointmentlog|length %} //it return length
{{foundappointmentlog[total-1].appointment_result}}  // here you get your last value using index 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文