We generally write a section file in shopify to include it somewhere. And one section data gets saved and the output is same data. Additionally there is block concept in schema from where we can add blocks dynamically. Blocks are same sets of options but in a multiple numbers.
Basically the requirement of this was generated when i have to have sections on numerous pages but with same structure & different data. If your requirement is the same case then you are at right place.
- I have created two pages for the demo purpose. Page 1 and Page 2 and assigned those on main menu via navigation sections.
- Those two pages use same page template custom.page. [This is created from `Online store > Edit HTML/CSS > add new template > select page type and name it custom` so this would generate page.custom.liquid template.]
- Assign this page.custom.liquid template to Page 1 & Page 2 [Edit Page 1 & Page 2, change template suffix from default page to `page.custom`]
- Until now we differentiated the Page 1 and Page 2 template from other different pages. Now let’s add a common section on custom.page template.
<div class="page-width"> <div class="grid"> <div class="grid__item medium-up--five-sixths medium-up--push-one-twelfth"> <div class="section-header text-center"> <h1>{{ page.title }}</h1> </div> <div class="rte"> {% section 'dynamic-section' %} {{ page.content }} </div> </div> </div> </div>
On the code above we just included `{% section ‘dynamic-section’ %}` section. I have added it just above the page content area. You can place it above or below as according to your requirement.
5. Create a dynamic-section.liquid section template under sections. [Edit HTML/CSS > Under Sections > Create template]
6. On dynamic-section.liquid
template add following code.
<!--/ page handle diffrentiates with each page --> {% assign page_handle = page.handle %} {% if section.blocks.size > 0 %} {% for block in section.blocks %} <!--/ ONLY print specific page data --> {% if block.settings.page_handle == page_handle %} {{ block.settings.page_title }} <!--/ To print content of particular page --> {% comment %} {% assign page_content = block.settings.page_handle %} {{ pages[page_content].content }} {% endcomment %} {% endif %} {% endfor %} {% endif %} {% schema %} { "name": "Page Management", "class": "index-section", "settings": [ { "type": "text", "id": "title", "label": "Common Title" } ], "blocks": [ { "type": "text_block", "name": "Page Management", "settings": [ { "type": "page", "id": "page_handle", "label": "Select a page" }, { "type": "text", "id": "page_title", "label": "Page title" } ] } ] } {% endschema %}
The logic here is i have compare the page handle and only printed from the block whose page handle matches. Cheers !!
Following are the screenshots of Theme customization section. To view result Navigate to `Online store > Edit html/css > Customize theme` Navigate to page 1 from Top menu(we added those page 1 and page 2 from navigation)
Screenshot 1
Screenshot 2
21 Comments
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Anthony
February 19, 2018
Samual, Is it possible to do a Shopify multiple sections/schema for multiple pages. Basically, I want all the custom sections of my home page available for making product landing pages. Venture theme. Current apps do not let me mimic my home page without a lot of customization and monthly fee.
Suman K.C
February 20, 2018
Hi Anthony, Yes we can make the homepage sections available for product landing page by including respective sections liquid template on pages we need. Please let me know if you need further guidance on doing this.
Max L
February 21, 2018
A pretty smart solution, I like this idea much better than creating a bunch of different templates and duplicate code.
Adolfo Mario Trevino Aguilar
June 27, 2018
Greetings from Mexico, We had to create a lot of different landing pages for a customer, your solution worked perfect for us!
Thank you so much!
Suman K.C
July 5, 2018
Hi Adolf,
That’s great news. I’m happy that it is helpful for you. Cherrs !!
stephanos
July 18, 2018
This is a great article and i like the technique but the only thing is that I’ve noticed is that you get the blocks from page 2 in page 1 theme editor and visa versa is there a way to only show the blocks relevant to the page as well as the content
Suman K.C
July 18, 2018
Hi Stephanos,
I guess you get it wrong. Page 1 and Page 2 are independed pages which has template suffix `custom.page` applied to them. As they are using the same template they both include a section template `dynamic-section`.
stephanos
July 18, 2018
Well in your second screenshot you have the page management blocks twice and they relate to different pages. You are on page 2 yet you have the block for page 1 page management as well.
Suman K.C
July 19, 2018
Hi Stephanos,
For all the pages which use `custom.page` template suffix will have this dynamic block section appeared on theme customization sidebar. And in this dynamic block addition section you add block for pages but only page specific data will be appeared on page frontend. In the screenshot first block is added for Page 1, and 2nd block is added for Page 2. And both these blocks can be seen on theme customization sidebar of both pages.
stephanos
July 19, 2018
Yes exactly I was wondering if there is a way to only show the blocks relevant to the page you are on in the theme editor. I understand that it is probably a tall order and might not be possible. My use case for your method was for a reusable hero top banner. I want to build one banner liquid section, one schema for it and reuse it with different data on multiple pages, just as the top banner. However I would’ve liked to only show the banner blocks that apply to those pages if that makes sense.
Abhishek
July 30, 2018
Hello Suman,
I have a question: Is it also possible to provide name of the dynamic section in the customizer itself? i.e. instead of “Page Management” for each of the title block, is it possible to give a dymanic name such as for Page 1 “Page management for Page 1” and for Page 2 “Page management for Page 2” etc.
Suman K.C
August 7, 2018
Hi Abhishek,
Unfortunately that’s not possible. Because as you can see its the name of block so it’s not possible.
John Abelardo M.
November 14, 2018
Hi Suman K.C and Abhishek,
I found out that the Abhishek’s idea is possible, by just adding:
{
“type”: “text”,
“id”: “title”,
“label”: “Title here!”
},
id with “title” will automatically change the “Page Management” of the block.
I added that inside the Page Management block.
{
“type”: “text_block”,
“name”: “Page Management”,
“settings”: [
{
“type”: “text”,
“id”: “title”,
“label”: “Title here!”
},
{
“type”: “page”,
“id”: “page_handle”,
“label”: “Select a page”
},
{
“type”: “text”,
“id”: “page_title”,
“label”: “Page title”
},
{
“type”: “html”,
“id”: “custom_html”,
“label”: “Custom HTML”,
“default”: “HTML”
}
]
}
You will only specify it each time you add. But it will help you find the page more quickly though.
Thanks, Suman for this wonderful idea.
Suman K.C
November 14, 2018
Hi John,
That’s a cool solution 🙂
Thank you for mentioning and i’m glad you liked the idea.
Cheers!
Mark
February 6, 2019
How could I adapt this to work on product pages? Thanks in advance for any input, it’s really helpful!
Suman K.C
February 8, 2019
Hello Mark, on products also this is possible comparing the product handle instead of page id. But this won’t be application if you have larger number of products.
Chanel
March 25, 2019
Thanks for this code. It was a great starting point for me to create a section to display our digital magazines. I already did the code to create the digital magazine as sliding images, but I didn’t know how to get the code to only show for each individual magazine. I have them setup as blog articles. Took some reading to understand the settings for blocks and how to structure it. Thanks so much for this very helpful.
Dave
May 15, 2019
This is one of the most elegant solutions I’ve seen to solve this problem. Absolutely brilliant.
Jesse
May 17, 2019
I’m about to use this for a site where I thought I was gonna have millions of templates and millions of sections. Wish me luck.
Sean
November 23, 2020
This worked perfect and one of the only viable solutions on the internet as of November 23, 2020. Thank you!
Faisal Mahmood
November 24, 2020
Hi, I have created a custom page template and added a custom section with blocks, I am using different snippets to render different functions but i am facing problem while adding slideshow, Could you guide me for this issue?
Leave a Reply