Search code examples
jekyllgithub-pagesliquid

Use variable inside Liquid tag call instead of string


I have already checked this solution and it doesn't seem to work for my problem. I have issue passing post.image variable name to tag responsive_image. If I pass string like that {% responsive_image path: assets/img/ar-7.jpg %} it works without any issue but I didn't found a way how to pass variable to that. Any ideas?

1) I thought this would work, unfortunately string post.image is passed instead of variable. Commented code is working example that I need to change to responsive image.

 {% if post.image %}
  {% responsive_image path: post.image %}
  <!-- <img class="has-ratio" src="{{post.image}}" /> -->
 {% endif %}
Invalid image path specified: "post.image" 
  Liquid Exception: unable to open image `/Users/.../Documents/Apps/Jekyll/wtc-mbp/post.image': No such file or directory @ error/blob.c/OpenBlob/2881 in .html

2) Solution from this answer, doesn't work

 {% if post.image %}
  {% assign path = post.image %}
  {% responsive_image path %}
 {% endif %}
Invalid image path specified: nil 
  Liquid Exception: no decode delegate for this image format `' @ error/constitute.c/ReadImage/566 in .html

3) Another idea also doesn't work

 {% if post.image %}
  {% assign path = post.image %}
  {% responsive_image path: path %}
 {% endif %}
Invalid image path specified: "path" 
Liquid Exception: unable to open image `/Users/.../Documents/Apps/Jekyll/wtc-mbp/path': No such file or directory @ error/blob.c/OpenBlob/2881 in .html

Solution

  • To use Liquid variables, you need to opt for the responsive_image_block tag instead:

    {% responsive_image_block %}
      path: {{ post.image }}
    {% endresponsive_image_block %}