0
votes

I have a question about object menus which is similar to linklist. It has a weird behavior.

Here is the code. Lines 6-9 contains 4 similar instructions to get menu from the menus object:

{% for link in linklists.main-menu.links %}
  {% if collection.handle == link.handle %}
    <p>test</p>
  {% endif %}
  {% assign linkTitleHandleize = link.title | handleize %}
  <p>menu[collection.handle] returns {{ menus[collection.handle] }}</p>
  <p>menu["bags"] returns {{ menus["bags"] }}</p>
  <p>menu[link.handle] returns {{ menus[link.handle] }}</p>
  <p>menu[linkTitleHandleize] returns {{ menus[linkTitleHandleize] }}</p>
{% endfor %}

So the html output is:

test

menu[collection.handle] returns EmptyDrop

menu["bags"] returns EmptyDrop

menu[link.handle] returns LinkListDrop

menu[linkTitleHandleize] returns LinkListDrop

For some reason, menus object returns expected "bags" menu array only when key is link.handle or transformed link.title | handleize (keep in mind that variable linkTitleHandleize is just a plain string) .

If I'm trying to pass same string but by collection.handle or even by plain string (in my case it is "bags") then menus returns nothing. I'm very confused, since the strings are equal (see condition on line 2 - it returns true and "test" is rendering) This is a very strange behavior and before this day I thought I know Liquid very well.

UPD: I have to rephrase my question. It is not about how to create a working version of code. It is a sort of research: why Liquid returns EmptyDrop for menu[collection.handle] and for menu["bags"] and why it returns LinkListDrop for menu[link.handle] and for menu[linkTitleHandleize] in case when linkTitleHandleize = link.handle = collection.handle = "bags". Thank you!

1
the linklist object is the "menu", "linklist objects are called "menus" in the Navigation page of the Shopify admin." - docs menus is just what linklist objects are referred to in the shopify admin uiJonathan Portorreal
Yes, thank you. I saw this reference in the Shopify official docs.Maksim C

1 Answers

1
votes

There is no menus object in liquid.

I don't know from where you got that object reference.

The code should be as follow.

{% for link in linklists.main-menu.links %}
  {% if collection.handle == link.handle %}
    <p>test</p>
  {% endif %}
  {% assign linkTitleHandleize = link.title | handleize %}
  <p>linklists[collection.handle] returns {{ linklists[collection.handle] }}</p>
  <p>linklists["bags"] returns {{ linklists["bags"] }}</p>
  <p>linklists[link.handle] returns {{ linklists[link.handle] }}</p>
  <p>linklists[linkTitleHandleize] returns {{ linklists[linkTitleHandleize] }}</p>
{% endfor %}

Please note that it will return just [Object] since you are referring to https://help.shopify.com/en/themes/liquid/objects/linklist