Wanting to check whether a block type has been created in a section

Hello,

I’m building an ingredients page that has the alphabet at the top as tabs and the content for each letter underneath.

I’ve set up block types to manage this, ie:


- A

- B

- C

{% for block in section.blocks %}
    {% if block.type == 'A' %}
    {% include 'ingredientItem' %}
    {% endif %}
    {% endfor %}

{% for block in section.blocks %}
    {% if block.type == 'A' %}
    {% include 'ingredientItem' %}
    {% endif %}
    {% endfor %}

{% for block in section.blocks %}
    {% if block.type == 'A' %}
    {% include 'ingredientItem' %}
    {% endif %}
    {% endfor %}

What I would like to do is to check if there has been a block type created and only apply my link to the letter in the list if that block type exists.

eg:


- {% if block.type == A%}A{% else %}A{% endif %}

{% for block in section.blocks %}
    {% if block.type == 'A' %}
    {% include 'ingredientItem' %}
    {% endif %}
    {% endfor %}

But obviously, what I have here doesn’t work. I was looking for some help as to how to check for a block type’s existence.

Thanks for any help you can provide.

2 Likes

@EllouiseHNuebar

sorry for this issue can you please try maybe helpful this

http://www.codeshopify.com/blog_posts/shopify-sections-with-dynamic-blocks

Just answering this for posterity as I ran into this issue and there weren’t too many helpful answers out there. But you can just do another {% for block in section.blocks %} for the check you want to do:


- {% for block in section.blocks %}
    {% if block.type == A and flag == false%}
      A
    {% else %}
      A
    {% endif %}
  {% endfor %}

{% for block in section.blocks %}
    {% if block.type == 'A' %}
    {% include 'ingredientItem' %}
    {% endif %}
    {% endfor %}

If you only want it to happen once no matter how many blocks they may have added, you’ll need to add a flag:


- {% assign flag = false %}
  {% for block in section.blocks %}
    {% if block.type == A and flag == false%}
      A
      {% assign flag = true %}
    {% else %}
      A
    {% endif %}
  {% endfor %}

{% for block in section.blocks %}
    {% if block.type == 'A' %}
    {% include 'ingredientItem' %}
    {% endif %}
    {% endfor %}