在Liquid标签中使用过滤器

时间:2011-05-12 00:37:48

标签: filter tags liquid jekyll

我正在使用jekyll和Liquid在github页面上生成一个静态网站。

我希望根据文档中的内容量是否达到特定数量的作品来做出一些内容决策。 jekyll有一个液体过滤器,用于计算我想在if标签中使用的单词数。我试过这个:

{% if page.content | number_of_words > 200 %} 
    ...
{% endif %} 

但它似乎不起作用。我还尝试将结果分配给变量并使用它,并捕获过滤器的输出。但到目前为止,我没有运气。

有人设法在液体标签中使用过滤器吗?

4 个答案:

答案 0 :(得分:25)

{% assign val = page.content | number_of_words %}
{% if val > 200 %}
 ....
{% endif %}

答案 1 :(得分:18)

我认为不可能在标签内部使用过滤器;它似乎不太可能。

但是,我设法建立了一套可以解决您特定问题的条件(辨别页面长度或短于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 %}

问候!

答案 2 :(得分:0)

{% capture number_of_words_in_page %}{{page.content | number_of_words}}{% endcapture %}
{% if number_of_words_in_page > 200 %} 
    ...
{% endif %} 

试试这个。

答案 3 :(得分:0)

刚刚找到https://github.com/mojombo/jekyll/wiki/Plugins,其中提供了有关如何为Github编写自定义标记的详细信息。这似乎是一个可能的方向,并提供访问其他开发人员的许多其他自定义。