Search code examples
symfonytwigsymfony-sonata

Render dynamic block name with Sonata Block Bundle


Does anyone know how to render a dynamically generated block name using the Sonata Block Bundle? An example of what I am trying to achieve is:

page.html.twig:

<div class="content">
 {{ sonata_block_render({
'name': '/content/blocks/{{ suffix }}'
}) }}
</div>

where suffix is a variable passed to the twig template, e.g. about-us. This allows me to use a single template for rendering all my CMS content. I have tried doing the above, but that doesn't seem to work.


Solution

  • You're already in twig context, that's why {{ }} didn't work. You'll need the string concatenation operator (~) like this:

    <div class="content">
    {{ sonata_block_render({
        'name': '/content/blocks/' ~ suffix
    }) }}
    </div>