Search code examples
jekyllliquidjekyll-extensions

Liquid variable in Jekyll frontmatter ... and use it in a Jekyll plugin


I very like the possibility to use Liquid variables in Jekyll frontmatter through the jekyll-liquify plugin:

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

Liquid::Template.register_filter(LiquidFilter)

works great, but i want to go one step further and read the image size with the plugin jekyll-image-size from an image that is given as a liquid variable in frontmatter

image: '{{ page.myimage }}'

... so i need to implement the above parser in the jekyll-image-size plugin

...
class ImageSizeTag < Liquid::Tag

  def initialize(tagName, content, tokens)
    super
    @content = content.strip
    @tokens = tokens
  end
...

I don't know ruby so i have no clue how to do that, but i guess it shouldn't be too complicated. Thanks for your help!


Solution

  • pretty simple solution using assign or capture and a hook or the liquify plugin for jekyll:

    {% assign image = page.myimage | liquify %}
    {% imagesize image:size %}
    

    or

    {% capture image %}{{ page.myimage | liquify }}{% endcapture %}
    {% imagesize image:size %}
    

    ... just needed if you want to use a variable in frontmatter to include an image from another variable and use the jekyll-image-size plugin... might be useful for other plugins as well.

    https://github.com/gemfarmer/jekyll-liquify

    http://acegik.net/blog/ruby/jekyll/plugins/howto-nest-liquid-template-variables-inside-yaml-front-matter-block.html