Capture content_for 'block'

Hi there,

I’m currently working on a menu for one of my clients and I’ll need the following, I need transfer a content_for ‘block’, id: ‘test’, type: ‘_private_block_in_blocks_folder’ to my snippets, the thing is that I have one block for Mobile content and another one for desktop content, as I want to make it configurable, I use blocks for my menu, how could you achieve that since we’re not allowed to capture specific block and not allowed to use content_for in snippets ?

Have a great day
Kevin

Hi @Kevin_RIOU I’m not entirely sure if I’ve understood your situation correctly, but the workaround is to avoid pulling that entire content_for reference into your snippet at all. Instead, pass the block’s data down into the snippet via Liquid’s built-in snippet parameters. This way you don’t rely on content_for inside the snippet, nor do you need to capture its contents.

Your section:

{% schema %}
{
  "name": "Your section",
  "blocks": [
    {
      "type": "desktop_menu",
      "name": "Desktop Menu"
    },
    {
      "type": "mobile_menu",
      "name": "Mobile Menu"
    }
  ],
  "max_blocks": 2
}
{% endschema %}


{% for block in section.blocks %}
  {% case block.type %}
    {% when 'desktop_menu' %}
      <div class="desktop-menu">
        {% render 'menu-snippet' with block %}
      </div>
    {% when 'mobile_menu' %}
      <div class="mobile-menu">
        {% render 'menu-snippet' with block %}
      </div>
  {% endcase %}
{% endfor %}

so your snippet receives the block object from the section above and you can now access block.settings or other block attributes:

{% if block.type == 'desktop_menu' %}
  <!-- Specific markup for desktop goes here using block.settings as needed -->
  <h3>Desktop Menu</h3>
{% elsif block.type == 'mobile_menu' %}
  <!-- Specific markup for mobile here -->
  <h3>Mobile Menu</h3>
{% endif %}

Also you could conditionally show different markup inside the snippet if you want the same snippet to handle both desktop and mobile, or you can make them separate snippets.

Hope it helps!

He’s not talking about “normal” blocks… but theme blocks.