Can I pass a variable from a Jekyll template to include and then display the data from this variable?

In foo.html (post), I have the following:

{% assign table_name="application" %} {% include table.html %} 

This assignment works fine in table.html

 {% assign tableName = table_name %} <p>table name is: {{ tableName }}</p> # renders "table name is: application" 

Now I'm trying to sort the data array that I defined in config.yml by doing the following:

 {% for header in site.table.tableName.headers %} <th>{{ header }}</th> {% endfor %} 

It does not give me any result.

If I modify the for statement to include the contents of the variable, and not the variable, it works fine.

 {% for header in site.table.application.headers %} 

This makes me think that this is not a problem with my array, but that it is either a flaw in Jekyll, a bug in Jekyll, or I am not exactly constructing my statements.

Any idea how I could make this work?

+4
source share
1 answer

It seems like you can do it. I had to think about it more programmatically. It seems to be happening that Jekyll was expecting an object, and I fed it a string.

 {% assign tableName = "aStringName" %} {% include table.html %} 

So,

 # in _includes/table.html {% for header in site.table.tableName.headers %} 

interpreted as

 {% for header in site.table."aStringName".headers %} 

When I switched to the parenthesized entry for the object, that was perfect.

Final result:

 {% for header in site.table[tableName].headers %} 

or, as Jekyll sees,

 {% for header in site.table['aStringName'].headers %} 
+9
source

Source: https://habr.com/ru/post/1447671/


All Articles