Pass variable to a section tag {% section the_variable %}

fcis
New Member
1 0 2

Hi,

I know it's possible to pass a variable to an include tag, ex:

{% assign item = 'pre' | append: name %}
{% include item %}

I'm having issues replicating that behavior with the section tag, ex:

{% assign item = 'pre' | append: name %}
{% section item %}

*** This does not work.

Does anyone know why that is? Or does anyone know a workaround?

 

Thanks.

KarlOffenberger
Shopify Partner
1867 182 777


Variables created outside sections are not available within sections. Likewise, variables created in sections are not available outside sections. If a section includes a snippet, the snippet has access to the variables in the section.

Straight from the docs ;-) And no, passing variables or using with as with include tag isn't possible for sections.

Liked this post? You might also like our fantastic upsell apps Candy Rack and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
davidbitton
New Member
10 0 0

What context does the template run in? Is it the page? So, for example, a collections page with have a Collection variable. Is there a way to change the context in which the template runs is so as to change that collection object?

0 Likes
Omo
Tourist
3 0 3

NB - Our use case is to have sections inside pages - the sections would be static, so we'd need to vary the included section depending on the page. Hence we want a page template that will include a different section depending on the page content/title, rather than having to create new page templates for every page.

 

Also struggled with this - we wanted:

{% assign ingredients = page.content | append: '-story' %}
{% section ingredients %}

 

But this didn't seem to work. We have a workaround which is:

{% if page.content == 'product1' %}
  {% section 'product1-story' %}
{% elsif page.content == 'product2'  %}
  {% section 'product2-story' %}
{% endif %}

Here we just include the section prefix as the page content. Not perfect but it saves us having to create and assign a new template for every prefix. This way we just have one template, assign it to any relevant pages, and then create multiple sections. 

davidbitton
New Member
10 0 0

Thanks.

0 Likes
mfs-mindsize
Excursionist
20 0 5

Well, if a section is ultimately about some admin side definition and storing associated args, can't you add the section and then from there take those args and pass them to various layouts? The section then (kinda? sorta?) in the code is the scope of a group of settings / args, yes?

 

Not being able to pass args into a section is not ideal. 

0 Likes
AvidBrio
Shopify Expert
125 6 12

@KarlOffenberger 

I successfully use section variable   into snippets

{%- comment -%}
i can access the section schema value into my snipeest
{%- endcomment -%}


{% assign menu = linklists[section.settings.side_menu] %}

<p> {{menu}}</p>
If you find our comment helpful, hit the like button and accept it as a solution.
Want us to implement custom changes in your store? Contact us
Email me directly - jim@avidbrio.com
0 Likes