Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Is there a way I can check if a section/block exists on a page and if so do something?
i.e. {% if section <name of section> exists on this page %} Do something {% endif %}
The theme in use is Dawn v4.0
Solved! Go to the solution
This is an accepted solution.
@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.
Contact [email protected] for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
@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.
Hire a developer to fix out your issues.
It's customization work,
Thank you.
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.
This is an accepted solution.
@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.
Contact [email protected] for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
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 don`t get it how I can target a section`s ID
@kgbfashionista wrote:where I need to skip a part of code if there is a particular section on the page
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-t...
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.
Contact [email protected] for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
We recently spoke with Zopi developers @Zopi about how dropshipping businesses can enha...
By JasonH Oct 23, 2024A big shout out to all of the merchants who participated in our AMA with 2H Media: Holi...
By Jacqui Oct 21, 2024We want to take a moment to celebrate the incredible ways you all engage with the Shopi...
By JasonH Oct 15, 2024