Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct Hash in Jekyll/Liquid

I was wondering if is there any way to construct a hash variable in Jekyll/Liquid.

Maybe something like this:

{% assign x = { foo: 1, bar: 2 } %}
{{ x[foo] }}
{{ x[bar] }}
like image 920
Linlin Yan Avatar asked Jan 01 '15 13:01

Linlin Yan


1 Answers

There is no filters to work on hashes except the for loop. The only way to get a hash is from global or pages variables, data or collection.

A generator plugin can do some computing before rendering.

You can also manipulate arrays. For now only with push and unshift as pop and shift are changing their behavior in Jekyll 3.

Recipe

In _config.yml add a emptyArray: []

In your code just {% assign myarray = site.emptyArray %}.

You can now push and unshift anything in it like {% assign myarray = myarray | push: "toto" %} or any object/hash like page, post, data,...

like image 180
David Jacquel Avatar answered Jan 04 '23 14:01

David Jacquel