Use variable in a for cycle

Solved

Use variable in a for cycle

Delio
Shopify Partner
18 0 1

Hello,

I need to have a different menu based on a page variable, I don't know how to insert this variable into the for loop, for example:

 

{% assign menus = aggregate-menu %}
{%- for link in linklists.menus.links -%}

aggregate-menu is the menu handle.

 

How can I insert the menus variable? I have tried many solutions but none are effective

Accepted Solution (1)

nvchien
Shopify Partner
55 23 14

This is an accepted solution.

Hi Delio,

 

Please try to update like this:

{% assign menus = 'aggregate-menu' %}
{%- for link in linklists[menus].links -%}
...

 Also, you can see documentation here: https://shopify.dev/docs/api/liquid/objects/linklist

 

- If this solution helped you out, please consider accepting it as a solution and giving it a thumbs-up. Your feedback is valuable to me and the community!
- If you're feeling extra generous, you can always buy me a coffee . Your support helps fuel my Shopify knowledge and keeps the solutions flowing!
- You can also follow more tips and tricks from my blog.

View solution in original post

Replies 2 (2)

nvchien
Shopify Partner
55 23 14

This is an accepted solution.

Hi Delio,

 

Please try to update like this:

{% assign menus = 'aggregate-menu' %}
{%- for link in linklists[menus].links -%}
...

 Also, you can see documentation here: https://shopify.dev/docs/api/liquid/objects/linklist

 

- If this solution helped you out, please consider accepting it as a solution and giving it a thumbs-up. Your feedback is valuable to me and the community!
- If you're feeling extra generous, you can always buy me a coffee . Your support helps fuel my Shopify knowledge and keeps the solutions flowing!
- You can also follow more tips and tricks from my blog.
Delio
Shopify Partner
18 0 1

It works, thank you!