1

I have a question. How can I output the Childs from a Neo (Neo Plugin) Block?

With this code, i get the output for all level 1 blocks but i don't get a output for the deeper levels.

And how can I handle different child templates for the Output?

My idea is to have a switch with different case for the level 2 output. Because i've different child block options.

{% for block in entry.MatrixNeo.level(1) %}
    {% switch block.type.handle %}

        {% case 'title' %}
            <h2>{{ block.mainTitle }}</h2>


        {% case 'boxes' %}
             <h2>{{ block.mainTitle }}</h2>   

            {% for item in block.children %}
                {% switch block.type.handle %}

                    {% case 'box' %}
                        <h2>{{ block.mainTitle }}</h2>

                        {% case 'image' %}
                        <h2>{{ block.mainTitle }}</h2>

                {% endswitch %}
            {% endfor %}

    {% endswitch %}
{% endfor %}

Can somebody help me out, please?

=====================

UPDATE

Now my code works fine but i've a new questione:

How can i output a real Matix-Field in a child element (level 2 in Neo).

My Code:

{% nav block in entry.myField.level(1) %}
    {% switch block.type.handle %}
        {% case 'title' %}
            <hgroup>
                <h2>{{ block.mainTitle }}</h2>
                <h4>{{ block.subTitle }}</h4>
            </hgroup>

        {% case 'intro' %}
            <section class="block">
                {% for item in block.children %}
                    {% include "_includes/elements/" ~ item.type %}
                {% endfor %}
            </section>

    {% endswitch %}
{% endnav %}

The child Code:

<div class="box">
    <h2>{{ item.mainTitle }}</h2>

    {% for button in block.myButton %}
        {% switch button.type %}
            {% case 'buttonIntern' %}
                <a class="button" href="{{ button.linkIntern }}">{{ button.linkText }}</a>

            {% case 'buttonExtern' %}
                <a class="button" target="_blank" href="{{ button.linkIntern }}">{{ button.linkExtern }}</a>

        {% endswitch %}
    {% endfor %}

</div>
  • Is MatrixNeo a native Matrix field or a Neo field from the Neo plugin? And what type of field is children in block.children? – Brad Bell Aug 31 '16 at 19:08
  • Yes, the Neo-Plugin. My idea is to have a switch with different case in the level 2 output. Because i've different child block options. – The Frog Sep 1 '16 at 6:26
  • 1
    Per your updated question. When I see people putting a Matrix within a Neo my first question is why? I can't think of any reason(there might be some) that if you've committed to using NEO, you'd need a Matrix field. You could create a "button" block type in NEO which has two children; one External Link and one Internal Link and use the same code approach as the original answer. – AbbeyDesign Sep 5 '16 at 12:49
  • Per your updated code, maybe the issue is you have button.linkIntern as the link in your buttonExtern call. – AbbeyDesign Sep 5 '16 at 13:12
  • You are right, it make no sense at all. Now I build it as a Neo block, but I can't output the button in the 3th level with the some technic as the level 2 blocks. Can you tell me how to output the 3th level blocks. – The Frog Sep 8 '16 at 7:53
2

Your above code is not working because you are using "block" instead of "item" in your children. So...

{% for item in block.children %}
    {% switch item.type.handle %}

You don't necessarily need to do a switch here, you could do a series of includes and make their file names match your block type... so "_includes/neo/box.twig" and "_includes/neo/image.twig"

In the example below I am replacing "item" with "child" to explain it better.

{% for block in entry.MatrixNeo.level(1) %}
      <h2>{{ block.mainTitle }}</h2>
      {% for child in block.children %}
           {% include "_includes/neo/" ~ child.type %}
      {% endfor %}
    {% endfor %}
| improve this answer | |
1

You must call the inner blocks (level2, level3 and so on) like the first level

{% if entry.bodyMain is defined and entry.bodyMain | length %}
    {% nav block in entry.bodyMain.level(1) %}
        {% switch block.type.handle %}

            {% case 'accordion' %}
                <ul class="accordion accordion-2">

                    {% nav block in block.children %}
                        {% switch block.type.handle %}

                            {% case 'timeBlock' %}
                                <li>
                                    <a class="opener" href="#">{{ block.heading }}</a>
                                    <div class="slide">
                                        {{ block.body }}
                                    </div>
                                </li>

                        {% endswitch %}
                    {% endnav %}

                </ul>

        {% endswitch %}
    {% endnav %}
{% endif %}

Or you can do it like this:

{% if entry.bodyMain is defined and entry.bodyMain | length %}
    {% nav block in entry.bodyMain.level(1) %}
        {% switch block.type.handle %}

            {% case 'accordion' %}
                <ul class="accordion accordion-2">
                    {% for block in block.children %}
                        {% include "_includes/_blocks/" ~ block.type %}
                    {% endfor %}
                </ul>

        {% endswitch %}
    {% endnav %}
{% endif %}

For that you must build a file with this content:

<li>
    <a class="opener" href="#">{{ block.heading }}</a>
    <div class="slide">
        {{ block.body }}
    </div>
</li>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.