Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using filters in tags #57

Closed
drekka opened this issue May 12, 2011 · 4 comments
Closed

Using filters in tags #57

drekka opened this issue May 12, 2011 · 4 comments

Comments

@drekka
Copy link

drekka commented May 12, 2011

I want to be able to use a filter inside a {% if expression %}. I've tried all sorts of things but cannot figure out if it's possible. Here is what I've tried:

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

Have I got the syntax wrong? or is this supported? or is there a bug?

@mhw
Copy link
Contributor

mhw commented May 12, 2011

Can you use the capture tag to put the output of the filter chain into a variable? Something like this:

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

@drekka
Copy link
Author

drekka commented May 13, 2011

I tried that, but it saved the rsult as a string so the if statement could not do the comparison. Is there something I can do to get liquid to see a string as a number?

@drekka drekka closed this as completed May 13, 2011
@drekka drekka reopened this May 13, 2011
@mhw
Copy link
Contributor

mhw commented May 14, 2011

Hmm; it doesn't look like it, at least not using that kind of syntax and approach. As you say, capture always assigns a string to the variable, so that won't work, and the if tag doesn't support filters so the original syntax you suggest won't work either.

A kludge that might work would be to define a filter called something like gt that worked along the lines of the existing plus and minus filters, then you should be able to do something like:

{% capture too_long %}{{ page.content | number_of_words | gt: 200 }}{% endcapture %}
{% if  too_long == "true" %}
   ...
{% endif %}

Slightly less of a kludge might be to add a content_word_count method to the page drop, if you can.

Another option is the following hack:

{% capture truncated %}{{ page.content | truncatewords: 200, '' }}{% endcapture %}
{% if truncated != page.content %}
   ...
{% endif %}

I agree, though, that it would be useful if the syntax was a bit more flexible and didn't require these hacks.

@drekka
Copy link
Author

drekka commented May 16, 2011

Thanks, I'll give those a try. I agree that they are "kludgy". I cannot modify or define anything because this is all being uploaded to github and they control jekyll, not me. :-(

I don't know if it's practical or not, but having tags be able to run embedded filters or at least have capture set a type appropriate to the return value would be really useful.

@fw42 fw42 closed this as completed Jun 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants