[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: Array
  • post: 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