Search code examples
twigblockadvanced-custom-fieldstimberwordpress-gutenberg

Gutenberg Blocks with Timber and ACF Flexible Content


We're creating a block.twig template as per: https://timber.github.io/docs/guides/gutenberg/#how-to-use-acf-blocks-with-timber to use ACF Flexible Content Field Types to create Gutenberg Blocks.

For reference the following sample code would be for Repeater Field Types:

{% for field in fields.repeater %}
    Title: {{ field.title }} <br/>
    Url: {{ field.url }}
{% endfor %}

which we tested and seems to work fine.

What would be the equivalent using Flexible Content fields?

Thanks.


Solution

  • @Rafael they're a little more complicated, just need to handle them like arrays. Let's pretend your Flexible Content field is called my_fc_field inside of it. You created 2 layouts, each has a single text field, one called tf_one, the other fc_two

    {% for subfield in fields.my_fc_field %}
        Layout name: {{ subfield.acf_fc_layout }}
        Text Field Value: {{ subfield.tf_one ? subfield.tf_one : subfield.tf_two }}
    {% endfor %}
    

    doing a {{ dump(fields.my_fc_field) }} should make it clear what's inside there that you can manipulate/output