Using a variable from a Section in a Snippet

New Member
7 0 0

Hi all,

 

Probably an easy one, but I can't seem to get a variable from a Section to appear in my Snippet. Variable from Snippet to Snippet works fine.

 

Here's the situation: I want to select certain collections and display them in a different way. Here's how the "check_variants" snippet works to check whether the current collection should be defined as a special collection:

 

{% assign variantdisplaytype = 'normal' %}
{% capture collection-display-handles %}
collection-one, collection-two, collection-three
{% endcapture %}
{% if collection-display-handles contains collection.handle %}
{% assign variantdisplaytype = 'special' %}
{% endif %}

Then in my collection_grid file, I simply use:

 

{% include 'check_variants' %}
{% if variantdisplaytype == 'special' %}
{{ variantdisplaytype }} <!--- SHOWS ON FRONT END ---> etc etc {% endif %}

This works!

 

However, if I use a section ("show_all_variants"), I can only output the variable within the section, not from the main grid snippet.

 

{% assign variantdisplaytype = 'normal' %}

{% capture collection-display-handles %}

{% for block in section.blocks %}
{{ block.settings.variant-collection }}, 
{% endfor %}

{% endcapture %}
{% if collection-display-handles contains collection.handle %}
{% assign variantdisplaytype = 'special' %}
{% endif %}


{{ collection-display-handles }} <!--- OUTPUTS FINE! --->
{{ variantdisplaytype }} <!--- OUTPUTS FINE! --->


{% schema %}
   {
    "name": "Collection Variant View",
	"class": "sb-collection-variants",
    "settings": [
    {
        "type": "paragraph",
        "content": "Choose the collections you'd like to view by variant rather than single product."
        }
  ],
    "blocks": [
      {
        "type": "description",
        "name": "Variant Collections",
        "settings": [
{
   "type":      "collection",
   "id":        "variant-collection",
   "label":     "Pick the collection you'd like to show all variants on."
}
        ]
      }
    ]
  }
{% endschema %}

{% stylesheet %}
{% endstylesheet %}

{% javascript %}
{% endjavascript %}

 

This does nothing in collection_grid:

 

{% section 'show_all_variants' %}
{% if variantdisplaytype == 'special' %}
{{ variantdisplaytype }} <!--- DOESN'T SHOW ANYTHING --->
etc etc
{% endif %}

Any help or workarounds gratefully received!

 

Thanks,

 

Tom

 

0 Likes