我正在使用jekyll和Liquid在github页面上生成一个静态网站。
我想根据文档中的内容量是否达到特定数量来做出一些内容决定。 jekyll有一个液体过滤器,该过滤器计算我要在if标记中使用的单词数。我已经试过了:
{% if page.content | number_of_words > 200 %}
...
{% endif %}
但是它似乎不起作用。我也尝试将结果分配给变量并使用它,并从过滤器捕获输出。但是到目前为止,我还没有运气。
有人设法在液体标签中使用过滤器吗?
编辑:这不再是最新的解决方案,请查看并投票赞成Martin Wang's assign
-based solution:
assign
[最初写此答案时(2011),{% assign val = page.content | number_of_words %}
{% if val > 200 %}
....
{% endif %}
>```
不是可行的解决方案,因为它不适用于过滤器。一年后,assign
引入了该功能。
如果有人需要在旧版本的Liquid中处理此问题,请在下面保留我最初的2011年答案。
我认为不可能在标签内使用过滤器;似乎似乎不可能。
但是,我设法建立了一组条件,可以解决您的特定问题(区分页面的长短是否超过200个字)。就是这样:
in 2012
为了使计算更加精确,使用{% 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 %}
刚刚找到{% assign val = page.content | number_of_words %}
{% if val > 200 %}
....
{% endif %}
,其中提供了有关如何为Github编写自定义标签的详细信息。这似乎是一个可行的方向,并提供了其他开发人员对许多其他定制的访问。