编辑:这不再是最新的解决方案,请参阅并支持Martin Wang 的assign
基于解决方案:
{% assign val = page.content | number_of_words %}
{% if val > 200 %}
....
{% endif %}
>```
在最初编写此答案时(2011 年)assign
不是一个可行的解决方案,因为它不适用于过滤器。该功能是在一年后的 2012 年推出的。
如果有人需要在旧版本的 Liquid 中处理这个问题,请在下面留下我 2011 年的原始答案。
我认为不可能以这种方式在标签内使用过滤器。这似乎是不可能的。
但是,我已经设法建立了一组可能解决您的特定问题的条件(判断页面是长于还是短于 200 个字)。就是这个:
{% capture truncated_content %}{{ page.content | truncatewords: 200, '' }}{% endcapture %}
{% if page.content != truncated_content %}
More than 200 words
{% else %}
Less or equal to 200 words
{% endif %}
为了使计算更精确,使用strip_html
运算符可能是明智的。这给了我们:
{% capture text %}{{ page.content | strip_html }}{% endcapture %}
{% capture truncated_text %}{{ text | truncatewords: 200, '' }}{% endcapture %}
{% if text != truncated_text %}
More than 200 words
{% else %}
Less or equal to 200 words
{% endif %}
问候!