Skip to content

Latest commit

 

History

History
63 lines (46 loc) · 3.14 KB

File metadata and controls

63 lines (46 loc) · 3.14 KB

Templating

Examples

Basic

<ol>
    {% for block in entry.neoField.level(1).all() %}
        {% switch block.type.handle %}
            {% case 'someBlockType' %}
                <li>
                    {{ block.someField }}
                    {% if block.children.all() is not empty %}
                        ...
                    {% endif %}
                </li>
            {% case ...
        {% endswitch %}
    {% endfor %}
</ol>

This is typically the most you'd need to know. Similar to how Matrix fields work, but with a notable difference. For Neo fields that have child blocks, you will first need to filter for blocks on the first level. It's essentially the same API as the craft.entries() element query.

Recursive

<ol>
    {% nav block in entry.neoField.all() %}
        <li>
            {{ block.someField }}
            {% ifchildren %}
                <ol>
                    {% children %}
                </ol>
            {% endifchildren %}
        </li>
    {% endnav %}
</ol>

Because Neo blocks have a level attribute, Neo fields are compatible with the {% nav %} tag.

Functions

craft.neo.blocks()

If you need to get Neo blocks in your template in a way that isn't connected to a Neo field value on a specific Craft element, you can use craft.neo.blocks(). This returns a Neo block query which can then be used in the same way as a typical Craft element query.

craft.neo.blockAnchorId()

This function takes a Neo block as its only argument, and outputs a string, based on the block's ID and the blockAnchorIdPrefix plugin setting, that is intended to be used as an HTML element ID. For example, using the default blockAnchorIdPrefix setting of 'blockAnchorId' and a block with id 12345, craft.neo.blockAnchorId(block) will return 'blockAnchorId-12345'. This should then be set as the ID of the HTML element representing the block in your templates; e.g. <div id="{{ craft.neo.blockAnchorId(block) }}">.

This functionality was inspired by the Matrix Block Anchor plugin.

craft.neo.previewHighlightAttribute()

This function takes a Neo block as its only argument, and outputs HTML attribute code when the template is rendered in Craft's preview mode. For example, if your block's ID is 12345, craft.neo.previewHighlightAttribute(block) will return 'data-neo-preview-highlight="12345"'. This allows highlighting of blocks in the content editor sidebar when their previewed content is hovered over, and scrolling the content editor sidebar to blocks when their previewed content is clicked, just like the Preview Mate plugin for Matrix fields that inspired this functionality.

More information

For a more in-depth breakdown of templating for Neo, see this issue.