简体   繁体   English

在 Liquid 标签中使用过滤器

[英]Using filters in Liquid tags

I'm using jekyll and Liquid to generate a static web site on github pages.我正在使用 jekyll 和 Liquid 在 github 页面上生成一个静态网站。

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. jekyll 有一个液体过滤器,它计算我想在 if 标签中使用的单词数。 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?有没有人设法在液体标签中使用过滤器?

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

EDIT: This is no longer the most current solution, see and upvote Martin Wang's assign -based solution instead:编辑:这不再是最新的解决方案,请参阅并支持Martin Wang 的基于assign的解决方案

 {% 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.在最初编写此答案时(2011 年), assign不是可行的解决方案,因为它不适用于过滤器。 That feature was introduced one year later, in 2012 .该功能于一年后,即2012 年推出

Leaving my original 2011 answer below in case someone needs to deal with this problem in older versions of Liquid.如果有人需要在旧版本的 Liquid 中处理这个问题,请在下面留下我 2011 年的原始答案。


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).但是,我设法建立了一组可能解决您的特定问题的条件(辨别页面是长于还是短于 200 个字)。 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.为了使计算更精确,使用strip_html运算符可能是明智的。 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!问候!

Just found https://github.com/mojombo/jekyll/wiki/Plugins which gives details on how to write a custom tag for Github.刚刚找到https://github.com/mojombo/jekyll/wiki/Plugins ,它提供了有关如何为 Github 编写自定义标签的详细信息。 This looks like a possible direction to go as well as providing access to many other customisations from other developers.这看起来是一个可能的方向,并提供对其他开发人员的许多其他定制的访问。

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

Try this.尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM