Reputation: 43
In a jekyll website, I'm trying to use an include as a multi-purpose template by passing multiple variables; not always all of them, not always the same number of variables.
From the page, I want to do this:
{% include multi-purpose.html var1='foo' var2='ble' var3='yea' %}
When I do this in the include.html, and when var1 has a value, it works:
var1 is {{ include.var1 }}
In the include file I want to be able to discriminate which variables are assigned, by doing something like this:
{% if var1 != null %}
this is my var1 {{ include.var1 }}
{% endif %}
{% if var2 != null %}
this is my var2 {{ include.var2 }}
{% endif %}
{% if var3 != null %}
this is my var3 {{ include.var3 }}
{% endif %}
etc...
Unfortunately, my syntax is wrong, this code doesn't render the code inside the if statements.
I also tried this:
{% if var1 != '' %}
this is my var1 {{ include.var1 }}
{% endif %}
{% if var2 != '' %}
this is my var2 {{ include.var2 }}
{% endif %}
{% if var3 != '' %}
this is my var3 {{ include.var3 }}
{% endif %}
etc...
This one renders everything, regardless it is assigned or not.
Upvotes: 4
Views: 1012
Reputation: 36421
To access the passed variables from inside the include, you need to prefix them with include.
Everywhere in the include!
Change this:
{% if var1 != null %}
to this:
{% if include.var1 != null %}
{% if var1 ...
doesn't work because it checks for a "local" variable (declared inside the include) named var1
(which doesn't exist, of course).
As explained in Mitja's answer, you don't need to check for null
or ''
, but you would still need to check {% if include.var1 %}
, not {% if var1 %}
!
Upvotes: 4