Variable in include param

Hi there.

I am using the alembic theme and there is a figure include and is used like this:
{% include figure.html image="/test.png" width="100%" %}

Now, I would like to add a front matter variable preview_image to a post that should be used in the excerpt of that post in the blog page in order to show an image in the excerpt.
So I edited the particular include post_list.html so it contains:

{% if page.preview_image %}
  {% include figure.html image="{{ page.preview_image }}" width="100%" %}
{% endif %}

Including this code results in an error message:

Liquid Exception: Invalid syntax for include tag. File contains invalid characters or sequences: figure.html image="/test.png" Valid syntax: {% include file.ext param='value' param2='value' %} in D:/Ruby/Ruby31-x64/lib/ruby/gems/3.1.0/gems/alembic-jekyll-theme-4.1.0/_layouts/blog.html

But if I hard code the image file name in the post_list.html addition like so:

{% if page.preview_image %}
  {% include figure.html image="/test.png" width="100%" %}
{% endif %}

it just works. But as you can see in the error message, the file name is resolved just right.

Not sure what to do. I tried to capture the variable before using it in the image parameter, but also didn’t work. Maybe there is some sort of resolution problem? Can someone help me out here?

>jekyll --version
jekyll 4.2.2

Don’t use brackets inside an if. You should do something like this:

{% if page.preview_image %}
{% include figure.html image=page.preview_image width=“100%” %}
{% endif %}

1 Like

That does the trick. I thought I tried every possible variation there…

Thanks a lot.

Adding a small correction to the answer above, the curly bracket is only supported for interpolating the include’s file name. The parameters shouldn’t have curly brackets irrespective of being enclosed by if blocks.