1
votes

I have a custom Liquid filter I use in a Jekyll site,

{{ page.url | git_modified }}

Which generates the modification date from the git log (plugin code here).

Often I may add the additional filter to convert this to a string or XML schema, depending on context, e.g. {{ page.url | git_modified | date_to_string }}. Everything is hunky-dory unless for some reason my git_modified filter fails to return a time object for some post. In that case, I am trying to write a decent fail condition but cannot quite figure this out.

I'd like to just wrap my call in a liquid if statement to check if the variable is defined first:

{% if defined?( {{ page.url | git_modified }} %}

But I don't seem to be able to use Liquid tags ({{) inside Liquid block options ({%, %}). I thought I could get around this with Liquid capture:

{% capture page_modified %}{{ page.url | git_modified }}{% endcapture %} 
{% if defined?(page_modified) %} 
{{ page.url | git_modified | date_to_string }}
{% endif %}

but said variables do not seem to be available to the if statements. Any suggestions?

1

1 Answers

4
votes

try doing it this way:

{% capture page_modified %}
    {{ page.url }}
{% endcapture %}

{% if page_modified %} 
    {{ page.url }}
{% endif %}

If page_modified isn't defined, its value will be nil anyway, so just use the if construct as you would in pure Ruby. I tested here with jekyll 1.0.0.beta2 — jekyll new test, then created a file with the above code — and it worked. :)