0
votes

I would like to set up a hash variable using strings from an array dynamically (instead of writing 1000 lines of code).

As well I would like to use a dynamically created string to access a hash by using it as the key - for a built-in (what I assume is a hash) object - settings. Settings allow you to access data in settings_schema.json, eg: settings.my_custom_setting

According to this documentation: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers "For hashes, the key must be a literal quoted string or an expression that resolves to a string."

so I have tried {% assign filter[thisFilter] = false %} but get an error: ParseError: illegal token

First issue / accessing hash key with a variable:

{% comment %} All possible filters {% endcomment %}
{% assign allFilters = "color,size,collection,style,height,function,artist" %}
{% assign allFiltersArray = allFilters | split ',' %}

{% comment %} hash of filters each set to false {% endcomment %}
{% for thisFilter in allFiltersArray %}
    {% assign filter[thisFilter] = false %}
{% endfor %}

Second issue, accessing settings object with a dynamically generated key:

{% comment %} set to true whichever filters are configured in settings for this collection {% endcomment %}
{% for thisCollection in allCollectionsArray %}
    {% if thisCollection == currentCollection %}

        {% for thisFilter in allFiltersArray %}
            {% assign itemToCheck = "filter_" | append: thisCollection | append: "_" | append: thisFilter %}
            {% if settings[itemToCheck] %}
                {% assign filter[thisFilter] = true %}
            {% endif %}
        {% endfor %}

    {% endif %}
{% endfor %}

In the first issue, I expect the result to be a hash such as: filter['color'] = false (or filter.color = false)? filter['size'] = false

In the second issue, I'm expecting something like: {% if settings.filter_shirts_color %}

1
Are you in a position where you can write metafields to a relevant Shopify object? Using a metafield on the shop or on the collection with a type of json_string could get you a lot closer to what you need?Dave B

1 Answers

1
votes

What you are trying to do is not possible. If you read further on your provided link Liquid for Designers, it is mentioned

Note that there is no way to write a literal array or hash as an expression; arrays and hashes must be passed into the template, or constructed obliquely with a tag or output statement.

Moreover, even if you have such hash, you cannot assign it new value. For example,

{% assign settings['some-setting-id'] = false %}

This will not work. Same is the case with array created using split filter. You cannot assign new values on any index.

For the second issue, this should work, the error in your case most probably is because of invalid generated string or there is no setting with that id. This should work fine and display value for that setting.

{%assign string_key = 'setting-key'%}
{{settings[string_key]}}

But this will not work because

{%assign string_key = 'setting-key'%}
{{settings.string_key}}

my_hash.key — Hashes also allow a shorter "dot" notation, where the name of the variable is followed by a period and the name of a key. This only works with keys that don't contain spaces, and (unlike the square bracket notation) does not allow the use of a key name stored in a variable.