[Jekyll](EN) Make array and add element in liquid
Summarize making array and adding element in Jekyll Liquid
Environment and Prerequisite
- Linux
- Jekyll
Jekyll Array Create and Add
Create
- Use
split
filter - It is used to convert comma-separated items from a string to an array but there is no string so it makes empty array.
{% assign ARRAY_NAME = "" | split: ',' %}
Add
- Use
push
to add element to array and reassign to original variable. push
does not actually add to current array so it needs to be reassigned.
{% assign ARRAY_NAME = ARRAY_NAME | push: ELEMENT %}
Example
- Example of making new array from specific collections in my blog.
opensource-contributions
: Arraypost
: Element
{% assign opensource-contributions = "" | split: ',' %}
{% for post in site.opensource-contributions %}
{% if post.post_id != page.post_id %}
{% assign opensource-contributions = opensource-contributions | push: post %}
{% endif %}
{% endfor %}
Reference
- https://jekyllrb.com/docs/liquid/
- https://shopify.github.io/liquid/filters/split/
- https://jekyllrb.com/docs/liquid/filters/
- https://shopify.github.io/liquid/tags/variable/
- https://shopify.github.io/liquid/tags/iteration/
- https://stackoverflow.com/questions/41263553/how-to-create-an-array-in-a-for-loop-in-liquid