在液体标签中使用过滤器 [英] Using filters in Liquid tags

查看:69
本文介绍了在液体标签中使用过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using jekyll and Liquid to generate a static web site on github pages.

我想根据一些内容决定来确定文档中的内容量是否达到特定数量的作品. jekyll有一个液体过滤器,该过滤器计算我要在if标记中使用的单词数.我已经尝试过了:

I want to base some content decisions on whether the amount of content in a document has reached a specific number of works. jekyll has a liquid filter which counts the number of words which I want to use in an if tag. I've tried this:

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

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

But it doesn't seem to work. I've also tried to assign the result to a variable and use that, and capture the output from the filter. But so far I've had no luck.

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

Has anyone managed to use a filter in a liquid tag?

推荐答案

这不再是最新的解决方案,请参见并投票而不是Martin Wang基于assign的解决方案:

This is no longer the most current solution, see and upvote Martin Wang's assign-based solution instead:

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

最初写此答案时(2011年),assign不是可行的解决方案,因为它不适用于过滤器.该功能于一年后推出, 2012 .

At the time this answer was originally written (2011) assign was not a viable solution since it did not work with filters. That feature was introduced one year later, in 2012.

在下面的版本中保留我最初的2011年答案,以防有人需要在较旧版本的Liquid中处理此问题.

Leaving my original 2011 answer below in case someone needs to deal with this problem in older versions of Liquid.

我认为不可能在标签内使用过滤器;看来似乎不可能.

I don't think it's possible to use filters inside tags that way; it just doesn't seem possible.

但是,我设法建立了一套条件,可以解决您的特定问题(区分页面的长度是200字还是短于200字).就是这样:

However, I've managed to build a set of conditions that might solve your particular problem (discerning wether a page is longer or shorter than 200 words). This is it:

{% 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运算符可能是明智的.这给了我们:

In order to make the calculations a little more precise, it might be wise to use the strip_html operator. That gives us:

{% 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 %}

致谢!

这篇关于在液体标签中使用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆