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

Highlighted
New Member
1 0 1

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.

1 Like
Highlighted
Shopify Partner
1838 174 631


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.

I turn coffee in to code - since 1998
0 Likes
Highlighted
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
Highlighted
Tourist
3 0 2

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. 

1 Like
Highlighted
New Member
10 0 0

Thanks.

0 Likes
Highlighted
New Member
4 0 0

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