Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using filters in Liquid tags

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?

like image 627
drekka Avatar asked May 12 '11 00:05

drekka


People also ask

What are liquid filters?

Liquid filters are porous mechanisms that enable filtration systems to remove solid particles from a fluid stream. Industrial processes rely on various fluids which can easily get contaminated by unwanted solids during various stages (e.g. during initial mixing or after certain chemical reactions).

What are Liquid tags?

Liquid tags are special elements of the Forem Markdown editor. They are custom embeds that are added via a {% %} syntax. Liquid is a templating language developed by Shopify. Liquid embeds are for tweets, like {% tweet 765282762081329153 %} or a Forem user profile preview, like {% user jess %} etc.

What is liquid markup?

Liquid is a templating language for rendering email and HTML. Liquid is the mechanism that enables the automated placement of data in comments and email notifications using placeholders. There are two types of markup in Liquid: Output, which is text output contained in double curly brackets.


2 Answers

{% assign val = page.content | number_of_words %} {% if val > 200 %}  .... {% endif %} 
like image 196
Martin Wang Avatar answered Sep 19 '22 15:09

Martin Wang


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!

like image 23
kikito Avatar answered Sep 21 '22 15:09

kikito