Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does conditional include stop working in Jekyll unless I add non-white space character(s)?

My Jekyll-based web site suddenly stopped including the Google Analytics script block. Yesterday, it was working (the script block was on all pages), but today, after I uploaded a new post, the script block was uniformly gone from all pages.

I'm hosting my site on GitHub pages, so in order to troubleshoot, I wanted to reproduce the issue on my local machine. It turned out that I was running old versions of Ruby and Jekyll on my local machine, so I updated Ruby and installed the GitHub Pages Gem in order to ensure that my local environment matched GitHub pages:

gem install github-pages

That enabled me to reproduce the issue. Now, even when I ran

jekyll serve --safe

the Analytics script block wasn't included in any page.

After much troubleshooting, I finally figured out that if I put any sort of non-white space character(s) in my JB/analytics file, between the google case and the include directive, it worked.

Thus, this works, in the sense that when the --safe switch is on, the script block in JB/analytics-providers/google is included:

{% case site.JB.analytics.provider %}
{% when "google" %}.
  {% include JB/analytics-providers/google %}

(notice the period character after {% when "google" %}).

However, this doesn't work - even with the --safe switch, the contents of include JB/analytics-providers/google is never included:

{% case site.JB.analytics.provider %}
{% when "google" %}
  {% include JB/analytics-providers/google %}

While I have a workaround, I'd much appreciate an explanation of this behaviour.

like image 813
Mark Seemann Avatar asked Sep 12 '13 16:09

Mark Seemann


1 Answers

This would indeed be an issue with liquid. But I can't reproduce it with the version of liquid that is current for github pages, 2.5.5 ...

Reproducing

This is what I tried, in a pry session:

# Load liquid 2.5.5
require 'liquid'

# Set up local file system to allow for includes
Liquid::Template.file_system = Liquid::LocalFileSystem.new( './' )

# Create template string
c = "{% case provider %}\n  {% when 'google' %}\n    {% include 'test' %}\n{% else %}\n  else\n{% endcase %}"

# Create template
t = Liquid::Template.parse c

# Render
t.render( { 'provider' => 'google' } )

Regardless of if I used \n or \r\n line endings I get the expected behaviour ( => "\n Liquid error: No such template 'test'\n" in this case since I didn't have a template, but it's calling the include so ... ).

Giving any other value than 'google' triggers the else case and results in => "\n else\n".

The addition of the period only affects the output by including the dot, not by if it triggers the include or not.

Followup

Could you check your versions? gem list liquid and gem list jekyll

Could you copy your site and strip it down to a minimal example for us to play with? It's difficult to reproduce what you got with the information given.

Note for experimenters

When executing Liquid in the console or from a test file remember that it likes it's hashes to be strings, not symbols. Feeding { provider: 'google' } to render above results in => "" ... It won't find your keys if they aren't strings.

like image 85
Jonas Schubert Erlandsson Avatar answered Sep 20 '22 13:09

Jonas Schubert Erlandsson