Conditional Sections > 2.0 Themes

Ceri-Waters
Shopify Partner
90 4 22

Hiya,

So in the current 1.0 setup you're able to do conditional sections.

As an example, if you wanted to bring in a different section depending on product type you can do:

 

            {%- if product.type contains 'Watch' -%}
                {% section 'product-watch' %}
            {%- else -%}
                {% section 'product' %}
            {%- endif -%}

 

 

Or, if you want to display aspects of the site differently for specific customers you can do:

 

            {%- if customer.tags contains 'Wholesale' -%}
                {% section 'header' %}
            {%- else -%}
                {% section 'header-wholesale' %}
            {%- endif -%}

 

 

My question is, how can you achieve this logic with the 2.0 structure since templates are now just JSON files. With the first example, you could achieve it by having a seperate template, but what about the second? In the second scenario, both sections ultimately have the same definition and output their HTML via a snippet - however it allows clients to control e..g the menu displayed for certain customers and so forth.

We've used this logic in quite a few bespoke themes, and I'm just wondering what's the best way to adapt this to 2.0.

Thanks in Advance,

Ceri.

0 Likes
NegativeSpace
Excursionist
18 3 5

My initial thought would be to move the conditional logic into the section file

0 Likes
Ceri-Waters
Shopify Partner
90 4 22

That would mean doubling up all of the section settings however. Currently I'm able to have one set of section settings for the header for e.g. retail customers, and another identical section file for wholesale. They then pass their data into a snippet which handles the rendering.

My only option at the moment is to include both sets of headers in the template, but only render the one which isn't ideal.

 

0 Likes
Ceri-Waters
Shopify Partner
90 4 22

Any other thoughts?

 

0 Likes