How can I check if a section or block exists on a Dawn v4.0 page?

Solved

How can I check if a section or block exists on a Dawn v4.0 page?

Sneaky
Shopify Partner
32 3 20

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

Accepted Solution (1)
PaulNewton
Shopify Partner
7450 656 1562

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


View solution in original post

Replies 8 (8)

Zworthkey
Shopify Partner
5581 642 1579

@Sneaky 
Welcome to Shopify Community.
Kindly explain properly what are you looking?


So that we can help you.
Thank you.

Sneaky
Shopify Partner
32 3 20

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.

Zworthkey
Shopify Partner
5581 642 1579

@Sneaky 

Hire a developer to fix out your issues.
It's customization work,

Thank you.

Sneaky
Shopify Partner
32 3 20

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.

PaulNewton
Shopify Partner
7450 656 1562

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
Shopify Partner
32 3 20

Yes, that is pretty much what I have discovered so far. I am looking at ID targetting as you have suggested.

kgbfashionista
Navigator
345 24 47

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

Feel free to like my post if it was helpful at all
PaulNewton
Shopify Partner
7450 656 1562

@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