【问题标题】:Jekyll Liquid limit containsJekyll 液体限制包含
【发布时间】:2018-11-01 17:23:12
【问题描述】:

我想限制显示包含布局项目的帖子的数量,但这样做只会限制我的帖子。如果我把 limit:4 放在 contains 之后,我会遇到;

Liquid 警告:Liquid 语法错误(第 50 行):预期 end_of_string 但在 /_layouts/home.html 中的“post.layout 包含“project”|limit:1”中找到管道

Liquid 警告:Liquid 语法错误(第 56 行):预期 end_of_string 但在 /_layouts/home.html 中的“post.layout 包含“project”limit:4 offset:1”中找到 id

希望这里的人很容易看出我哪里出错了。一个解释也很可爱。

{% for post in site.posts %} 
  {% if post.layout contains "project" | limit:1 %}
  <h1>{{post.title}}</h1>
  {% endif %}
{% endfor %}

{% for post in site.posts %}
  {% if post.layout contains "project" limit:4 offset:1 %}
  <h2>{{post.title}}</h2>
  {% endif %}
{% endfor %}

【问题讨论】:

  • 无法重现,但"post.layout contains "project" limit:1" 是有趣的部分。你有存储库网址吗?
  • @DavidJacquel 是的 (github.com/spskeen/sequel) 我只想展示我的“项目”帖子,但限制为 4 个。如果有更好的方法,我会这样做哈哈。

标签: if-statement jekyll liquid


【解决方案1】:

您只能在 for 循环中使用 limitoffset。 在这里,您在 if 语句中使用它,这就是您收到警告的原因。

更好的方法是使用 where_exp 过滤器。

  {% assign project-posts = site.posts | where_exp: "post", "post.layout contains 'project'"  %}

  {% for post in project-posts limit: 1 %}
    <h1>{{post.title}}</h1>
  {% endfor %}
  {% for post in project-posts limit:4 offset:1 %}
    <h2>{{post.title}}</h2>
  {% endfor %} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-01
    • 2020-05-15
    • 2013-01-07
    • 1970-01-01
    • 2016-04-14
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多