I’m trying to create a dynamic solution using metaobjects but struggling to get the syntax correct when referencing items of a specific metaobject definition using an assigned variable.
Let’s say I have 3 metaobjects “games_2024”, “games_2025”, “games_2026”. Each metaobject definition has the same structure: “description” and “game_date”. Creating many items in the appropriate metaobject will essentially create a list of games for each year.
I can easily reference each of the list of items by using the syntax:
{% for game in shop.metaobjects.games_2024.values %}
…do something custom like option input on product page so user can select game.
{% endfor %}
{% for game in shop.metaobjects.games_2025.values %}
…do something custom like option input on product page so user can select game.
{% endfor %}
But let’s now say that I want to make the solution dynamic by having a metafield definition on products that specifies the metaobject to use for pulling the game list. Field is a text input. Example: On the product the user would specify “games_2024” if they wanted all games for 2024.
In liquid code, I would like to iterate over the metaobject entries for the specified game list, but using the value from the product metafield instead of a hard coded value:
assign gamelist = “product.metafields.mynamespace.gamelist”
{% for game in shop.metaobjects.{{ gamelist }}.values %}
Unfortunately this code won’t compile.
I have also tried referencing using bracket notation but can’t get that to work either.
{% for game in shop.metaobjects.[‘{{gamelist}}’].values %}
I’m relatively new to liquid code so I hope I’m just not understanding the proper way to use the variable in the syntax. Thanks in advance.
