【问题标题】:Jekyll "Liquid syntax error" with limitJekyll“液体语法错误”有限制
【发布时间】:2017-12-01 16:54:24
【问题描述】:

我不知道这里出了什么问题,我还有其他几段具有相同结构的代码,但它们没有返回此错误。

这是我遇到问题的 HTML 文件 (item.html) 中的液体代码:

{% assign item-collection = site.item-collection | sort: 'date' | reverse %}
{% for item in item-collection %}
  {% if item.featured == true limit: 3 %}
    <div class="item">
    </div>
  {% endif %}
{% endfor %}

这是一个正在处理的“项目”(item.md);

---
layout: item
date: 2017-06-08 00:00:00
title: item 1
featured: true
tags:
---

这是终端返回的错误:

Regenerating: 1 file(s) changed at 2017-06-28 22:41:16     Liquid Warning: Liquid syntax error (line 30): Expected end_of_string but found id in "item.featured == true limit: 3" in /_layouts/item.html ...done in 1.337976 seconds.

如果我将日期留空,则不会发生此错误,但一旦输入内容,此错误就会停止构建网站。

如果我从液体代码中删除“限制:3”,错误也会消失,但我需要这个限制。

关于我做错了什么有什么想法吗? 提前致谢!

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    limit 是一个for 标记参数。它在特定索引处退出 for 循环。

    if 标签之后使用它没有任何意义,并且在处理它时会混淆 Jekyll。

    limit 标记移动到for 循环行,它应该只迭代前三个项目。

    {% for item in item-collection limit: 3 %}
     {% if item.featured  %}
    

    基于 cmets 的更新

    • 按特色标签过滤项目

      {% assign item-collection = site.item-collection | where_exp:"item","item.featured == true" %}
      
    • 按日期对结果进行排序

      {% assign item-collection = item-collection | sort: 'date' | reverse %}
      
    • 仅列出 3 个精选帖子

      <ul>
      {% for item in item-collection limit:3 %}
      <li>{{item.date}} - {{item.title}}</li>
      {% endfor %}
      </ul>
      

    总结:

    {% assign item-collection = site.item-collection | where_exp:"item","item.featured == true" %}
    
    {% assign item-collection = item-collection | sort: 'date' | reverse %}
    
    <ul>
    {% for item in item-collection limit:3 %}
    <li>{{item.date}} - {{item.title}}</li>
    {% endfor %}
    </ul>
    

    【讨论】:

    • 谢谢,这是有道理的,但我不希望 for 循环在循环浏览集合中的前三个项目后结束。我想要做的是过滤集合中带有“精选”标签的所有项目,并输出最多 3 个按日期排序的“精选”项目。有没有办法做到这一点?我试过按“特色”标签对集合进行排序,但这似乎没有任何作用。 {% 分配项目集合 = 站点.项目集合 | sort: features %} {% for item-collection %} 谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-11-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多