Is there a way I can check if a section/block exists on a page and if so do something?
i.e. {% if section exists on this page %} Do something {% endif %}
The theme in use is Dawn v4.0
Is there a way I can check if a section/block exists on a page and if so do something?
i.e. {% if section exists on this page %} Do something {% endif %}
The theme in use is Dawn v4.0
@Sneaky
Welcome to Shopify Community.
Kindly explain properly what are you looking?
So that we can help you.
Thank you.
Not sure how to explain it more.
I want to check if a section has been added to the page and if so do something. So, for example, on the home page, If the page has the section âFeatured Collectionâ on it, I want to have something where it checks if X section, in this case âFeatured Collectionâ is on that page, if so then do something, if the âFeatured Collectionâ section is not on the page then do nothing.
For example, I may want it to insert a specific piece of code if that section exists on the page for that specific section.
I realise it is custom, which is why I am trying to figure it out. I was just hoping someone could steer me in the right direction as so far I have not solved it, but still working on it.
Suggesting I hire someone is not helpful.
@Sneaky you wont be able to do this in liquid due to the way the templating system works and sections is scoped.
Layouts and other templates have no way of knowing if another template/section/snippet is in use.
An advanced hack is to capture the entire content_for_layout and string parse it for keywords.
On the frontend you can use javascript or CSS to act upon that sections existence.
Either by targeting that sections id or attributes that indicate the section type.
See the section dev docs to apply your own custom attributes to the sections container.
Yes, that is pretty much what I have discovered so far. I am looking at ID targetting as you have suggested.
Hi,
Have you been able to figure this out?
I am having the same issue, where I need to skip a part of code if there is a particular section on the page, which I could do using unless statement, but I dont get it how I can target a section
s ID
That generally means you should be using alternate templates
https://help.shopify.com/en/manual/online-store/themes/os20/theme-structure/templates#create-a-new-template
Otherwise this is a frontend javascript approach , where the sections #ID selector is used as the condition. var element = document.getElementById(âsection-template-124356euysgakjlq23543â) etc
In liquid you canât use an unless statement in a separate part of a theme to target a section object by itâs id.
There is no âsectionsâ* object representing all objects in a layout , *sections with an s plural, not a singular section object.
Advanced unsupported hack is parsing content_for_layout for strings.