Jekyll 杰基尔:结合极限、位置和反转

Jekyll 杰基尔:结合极限、位置和反转,jekyll,Jekyll,使用Jekyll,我想: 遍历所有页面 其中page.path不是当前路径 其中page.categories包含“特色” 将其反转(最近的第一个) 限制3 我在把所有的过滤器组装起来时遇到了问题 {% assign posts = site.posts | reverse %} {% for post in posts limit:3 %} {% if post.categories contains 'featured' and post.path != page.path %}

使用Jekyll,我想:

  • 遍历所有页面
  • 其中page.path不是当前路径
  • 其中page.categories包含“特色”
  • 将其反转(最近的第一个)
  • 限制3
我在把所有的过滤器组装起来时遇到了问题

{% assign posts = site.posts | reverse %}
{% for post in posts limit:3 %}
  {% if post.categories contains 'featured' and post.path != page.path %}
    {% include card.html post=post %}
  {% endif %}
{% endfor %}

目前,该限制无法正常工作,因为内部的
if
将阻止渲染一些项目

在进入循环之前,将
0
分配给
计数器
变量。不要在循环上设置限制,而是在
计数器
低于限制的情况下设置另一个条件,每次满足条件并输出卡时,使用液体的
plus
过滤器增加
计数器

{% assign posts = site.posts | reverse %}
{% assign counter = 0 %}
{% for post in posts %}
  {% if counter < 3 and post.categories contains 'featured' and post.path != page.path %}
    {% include card.html post=post %}
    {% assign counter = counter | plus: 1 %}
  {% endif %}
{% endfor %}
{%assign posts=site.posts | reverse%}
{%assign counter=0%}
{posts%%中的post为%s}
{%如果计数器<3且post.categories包含'featured'和post.path!=page.path%}
{%include card.html post=post%}
{%分配计数器=计数器|加:1%}
{%endif%}
{%endfor%}
一个更复杂的例子 所有这些都是基于我自己使用的循环。我的情况有点复杂:我检查当前页面是否有任何共享标记。下面我将此作为进一步的示例

{% assign counter = 0 %}
{% for post in site.posts %}
    {% if counter < 4 %}
        {% if post.url != page.url %}
            {% assign isValid = false %}
            {% for page_tag in page.tags %}
                {% for post_tag in post.tags %}
                    {% if post_tag == page_tag %}
                        {% assign isValid = true %}
                    {% endif %}
                {% endfor %}
            {% endfor %}
            {% if isValid %}
                {% include article_card.html %}
                {% assign counter = counter | plus: 1 %}
            {% endif %}
        {% endif %}
    {% endif %}
{% endfor %}
{%assign counter=0%}
{site.posts%中的post为%s}
{%如果计数器<4%}
{%if post.url!=page.url%}
{%assign isValid=false%}
{page.tags%}中的page_标记为%
{post.tags%}中的post_标记为%
{%if post_tag==page_tag%}
{%assign isValid=true%}
{%endif%}
{%endfor%}
{%endfor%}
{%如果有效%}
{%include article_card.html%}
{%分配计数器=计数器|加:1%}
{%endif%}
{%endif%}
{%endif%}
{%endfor%}
为什么
出现故障

尽管如此,它只能进行精确的比较,因此您必须像这样重新设计轮子,以便在更复杂的条件下实现类似于
where
的场景。这确实会导致大量的
站点循环。posts
,但是Jekyll作为一个预处理器意味着使用一些效率低下的即兴
,其中
类型的循环只是编译时的问题,而不是运行时的问题。即便如此,为了尽可能降低成本,我还是选择让Jekyll首先计算
计数器
条件。

什么是site.templates?看起来怎么样?@marcanuy抱歉,这是posts系列。没什么特别的。