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

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

问题描述

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

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

{% if page.content |number_of_words >200%}...{% 万一 %}

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

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

解决方案

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

<块引用>

{% assign val = page.content |number_of_words %}{% 如果 val >200%}....{% 万一 %}>```

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

如果有人需要在旧版本的 Liquid 中处理这个问题,请在下面留下我 2011 年的原始答案.

<小时>

我认为在标签中使用过滤器是不可能的;这似乎不可能.

但是,我已经设法建立了一组可能解决您的特定问题的条件(辨别页面是长于还是短于 200 个字).就是这样:

{% capture truncated_content %}{{ page.content |truncatewords: 200, '' }}{% endcapture %}{% if page.content != truncated_content %}200多个字{% 别的 %}少于或等于200字{% 万一 %}

为了使计算更精确,使用 strip_html 运算符可能是明智的.这给了我们:

{% 捕获文本 %}{{ page.content |strip_html }}{% endcapture %}{% 捕获 truncated_text %}{{ 文本 |truncatewords: 200, '' }}{% endcapture %}{% if text != truncated_text %}200多个字{% 别的 %}少于或等于200字{% 万一 %}

问候!

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

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?

解决方案

EDIT: 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 %}
>```

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.

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.

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

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

Regards!

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

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