Ad

Using A String To Create A Liquid Variable

- 1 answer

In my shopify store, I am using SuperFields in order to customize my site, though my question isn't about the app. On one of my pages, I need the value for the following:

variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key]

The value should be 0 or 1. If I evaluate the statement directly, such as:

{if variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key] =1%}

It throws an error when I render the page: Unexpected character '{'

I've also tried the following:

{% capture defaultImage %}variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key]{% endcapture %}
{% assign test = defaultImage %}

But 'test' is considered nil and doesn't return any value. I have tried to search for answers here and on the shopify forum, but, as my clumsy post title suggests, I'm having a hard time concisely searching for a solution to this problem. Any help is greatly appreciated.

Ad

Answer

You can try :

{% assign metafield-key = collection.title | downcase | prepend: "sf_" %}
{% assign key = variant.metafields[metafield-key][meta_tag_key] %}
{% if key == 1 %}
  Do the twist !
{% endif %}
Ad
source: stackoverflow.com
Ad