I have a file that is created for some of the posts in _includes
. If the file exists, I’ve added the file to the post’s metadata. If not, it’ll be empty.
This is my metadata:
---
layout: default
file_exist: filename/""
---
Now, I do not want to include the file if file_exist:
is empty. For this I have this:
{% if page.file_exist == empty %}
{% else %}
{% include /folder/{{ page.filename }} %}
{% endif %}
This throws the following error:
Error: Could not locate the included file '/folder/'
My understanding was, since the include liquid is in the else condition it wouldn’t look for the file, which is what I want. It looks like it still looks for the file even though it’s in an if condition.
Could someone help me with this?
I’m on jekyll 4.2.1.
Thank you!
The include
instruction accepts only the files under your _includes folder, no absolute paths should be accepted.
1 Like
Indeed.
So use this
{% include {{ page.filename }} %}
Or
{% relative_include /folder/{{ page.filename }} %}
Save yourself an empty block
Use unless
which is equivalent to if not
{% unless page.file_exist == empty %}
{% include /folder/{{ page.filename }} %}
{% endunless %}
1 Like
I haven’t done a check for empty like this.
I usually do this, to check that the key is set (even with a value of null or empty string)
{% if page.my_value %}
{{ page.my_value }}
{% endif %}
And then add a check that string is not empty string.
{% if page.my_value and page.my_value != '' %}
{{ page.my_value }}
{% endif %}
@MichaelCurrin I think you meant include_relative
instead…
- {% relative_include /folder/{{ page.filename }} %}
+ {% include_relative /folder/{{ page.filename }} %}
2 Likes