1
votes

So the current Shopify implementation of sections leaves a lot to be desired. The majority of the functionality is relegated to the homepage.

I'm trying to skirt around that to a certain degree but basically chucking all the section functionality (that would normally be split into multiple sections) into one section file, and then duplicating it for each product in the store, reusing the handle of each product as the section name.

E.g.: example-product-handle --> sections/example-product-handle.liquid

My idea was then to create, in the main product.liquid file, a simple routing system that would conditionally include a section if one exists that matches with the handle. This SO answer got my creative juices flowing.

The ideal result would look like...

{% assign current_page = product.handle %}
{% capture snippet_exists %}{% section current_page %}{% endcapture %}
{% unless snippet_exists contains "Liquid error" %}
    {% section current_page %}
{% endunless %}

This works beautifully for snippets. Replace section with include in that code, and the routing system performs perfectly.

With sections however?

Liquid syntax error: Error in tag 'section' - Valid syntax: section '[type]'

Is there no way around this? Do section names have to be explicitly stated?

1
I can't guide you how to make dynamic sections, but you can refer here - help.shopify.com/themes/development/theme-editor/… - HymnZzy
Dynamic section would be perfect, but the problem is this: "Sections can be dynamically added to the theme's homepage from the theme editor." They're only usable on the homepage. Elsewhere, you have to turn to static sections, whose contents are the same across each page they're included on. - Leland

1 Answers

2
votes

This isn't possible. It is purposefully not possible. Try instead using the section to dynamically include snippets.

{% for block in section.blocks %}
  {% case block.type %}
  {% when 'layout1' %}
    {% include 'layout1' %}
{% endfor %}