Re: Custom HTML wrapper for sections when update to 2.0

Solved

Custom HTML wrapper for sections when update to 2.0

Be_Steven
Shopify Partner
142 28 41

Hi everyone,

 

I'm struggling with finding a solution to migrate this templates/collection.liquid to templates/collection.json

<div class="container-a">
  {% section 'collection-sidebar' %}
</div>
<div class="container-b">
  {% section 'collection-promotions' %}
  <div class="container-c">
    {% section 'collection-template' %}
  </div>
</div>

 

On the new collection.json file I can't put sections in parent-child container like before.

 

Is there any solution for this on JSON template?


Thank you very much for your help

Was my reply helpful? Please Like and ✔️ Accept Solution. This mean alot to me.
I'm looking for a remote job. Please contact me via besteven0912@gmail.com
Accepted Solution (1)
Elizabeth_craig
Shopify Partner
13 2 9

This is an accepted solution.

I found a work-around using Javascript. 

You can create a function that will create a new div element, place it before the first child element you want to wrap, then you can append the child elements you want to wrap to the newly created parent div. Here is my solution for my specific case so you can modify it. Here, I wanted to wrap all of the sections with class "section-recent-titles" within a parent div.

 

 

// Wrap sections in a wrapper.
function wrapSections(section_class) {
  var childSections = document.querySelectorAll("."+section_class);
  var wrapper = document.createElement('div');
  wrapper.classList.add(section_class+"-wrapper");
  childSections.item(0).parentNode.insertBefore(wrapper, childSections.item(0));
  
  for ( var i=0; i< childSections.length; i++){
    wrapper.appendChild(childSections.item(i));
  }
}
// You can call this function with a specific section class.
wrapSections("section-recent-titles");

 

 

  

View solution in original post

Replies 7 (7)

jr-dev
Tourist
6 0 0

Have you found a solution? I'm struggling with the same problem...

Be_Steven
Shopify Partner
142 28 41

Hi Jr, 

Not yet, seems like no one care about this topic.

Was my reply helpful? Please Like and ✔️ Accept Solution. This mean alot to me.
I'm looking for a remote job. Please contact me via besteven0912@gmail.com

Be_Steven
Shopify Partner
142 28 41

Update:
I'm still have to face this issue when upgrading my client's theme from 1.0 to 2.0

Is there any idea please?

Was my reply helpful? Please Like and ✔️ Accept Solution. This mean alot to me.
I'm looking for a remote job. Please contact me via besteven0912@gmail.com
Elizabeth_craig
Shopify Partner
13 2 9

This is an accepted solution.

I found a work-around using Javascript. 

You can create a function that will create a new div element, place it before the first child element you want to wrap, then you can append the child elements you want to wrap to the newly created parent div. Here is my solution for my specific case so you can modify it. Here, I wanted to wrap all of the sections with class "section-recent-titles" within a parent div.

 

 

// Wrap sections in a wrapper.
function wrapSections(section_class) {
  var childSections = document.querySelectorAll("."+section_class);
  var wrapper = document.createElement('div');
  wrapper.classList.add(section_class+"-wrapper");
  childSections.item(0).parentNode.insertBefore(wrapper, childSections.item(0));
  
  for ( var i=0; i< childSections.length; i++){
    wrapper.appendChild(childSections.item(i));
  }
}
// You can call this function with a specific section class.
wrapSections("section-recent-titles");

 

 

  

Be_Steven
Shopify Partner
142 28 41

Thanks @Elizabeth_craig you are the only one who care about this topic with me 😄

I think using JS like that is the only solution until now. It may have an issue that it may conflict with other JS code that catch by class I think. 

But so far it's the only way.
Thank you mate!

Was my reply helpful? Please Like and ✔️ Accept Solution. This mean alot to me.
I'm looking for a remote job. Please contact me via besteven0912@gmail.com
almenko
Visitor
1 0 0

Hello! I'm having trouble figuring out exactly where to put the script. Could you tell me which file and how I need to add it?

Elizabeth_craig
Shopify Partner
13 2 9

Hey there,

 

You would put this code at the end of your theme’s JavaScript file, or better yet in a custom JavaScript file for your store. 

You should be able to copy the wrapSections function exactly as written. And then call the function with the last line, substituting “section-recent-titles” with “your-parent-section-name”. Every theme is different, so it might need some adjustments.

Hope that helps!