Include jekyll / liquid template data in a YAML variable?

Today I ran into a similar problem. As a solution I created the following simple Jekyll filter-plugin which allows to expand nested liquid-templates in (e.g. liquid-variables in the YAML front matter):

module Jekyll
  module LiquifyFilter
    def liquify(input)
      Liquid::Template.parse(input).render(@context)
    end
  end
end

Liquid::Template.register_filter(Jekyll::LiquifyFilter)

Filters can be added to a Jekyll site by placing them in the ‘_plugins’ sub-directory of the site-root dir. The above code can be simply pasted into a yoursite/_plugins/liquify_filter.rb file.

After that a template like…

---
layout: default
first_name: Harry
last_name: Potter
greetings: Greetings {{ page.first_name }} {{ page.last_name }}!
---
{{ page.greetings | liquify }}

… should render some output like “Greetings Harry Potter!”. The expansion works also for deeper nested structures – as long as the liquify filter is also specified on the inner liquid output-blocks. Something like {{ site.url }} works of course, too.

Update – looks like this is now available as a Ruby gem: https://github.com/gemfarmer/jekyll-liquify.

Leave a Comment