cuonghuynh
5/31/2018 - 4:34 PM

Shopify - create function to reuse

Shopify - create function to reuse

{% comment %}
  This function will find value responding to the given key
  params:
    - keys: array
    - values: array
    - key: mixed
    - default: mixed  // the default value if not found
  return:
    - value: mixed
{% comment %}

{% if default is defined %}
  {% assign value = default %}
{% else %}
  {% assign value = nil %}
{% endif %}

{% for k in keys %}
  {% if key == k %}
    {% assign value = values[forloop.index0] %}
    {% break %}
  {% endif %}
{% endfor %}

{{ value }}


// -------- Use -----------
{% assign size_titles = 'Large|Small|X-Large|X-Small|Medium' | split: '|' %}
{% assign size_handles = 'lg|sm|xl|xs|md' | split: '|' %}
{% assign tag_handle = 'lg' %}
{% include 'function.array.get-value' with {keys: size_handles, values: size_titles, key: tag_handle, default: tag_handle} %}
{% assign tag_title = value %}