Passing multiple variables to jekyll turns on and determines which ones are assigned

On the jekyll website, I'm trying to use include as a multipurpose template by passing in multiple variables; not always all of them, not always the same number of variables.

On the page, I want to do this:

{% include multi-purpose.html var1='foo' var2='ble' var3='yea' %}

      

When I do this in include.html , and when var1 is relevant, it works:

var1 is {{ include.var1 }}

      

In the include file, I want to be able to distinguish 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 does not display code inside 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...

      

It does everything, whether assigned or not.

Thank you for your help!

+3


source to share


2 answers


To access passed variables from within include, you need to attach them to include.


Everywhere in include!

Change this:

{% if var1 != null %}

      

:



{% 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 Mitya's answer, you don't need to check for null

or ''

, but you still need to check {% if include.var1 %}

, not {% if var1 %}

!

+3


source


Just check it like this:



{% if var1 %}

      

+1


source







All Articles