2. Twig Helpers

SonataBlockBundle provides several Twig helper functions to allow easier interaction with blocks:

Render a block from its instance:

1
{{ sonata_block_render(block) }}

Render a block by providing the block’s type and options:

1
2
3
4
{{ sonata_block_render({ 'type': 'sonata.block.service.rss' }, {
    'title': 'Sonata Project\'s Feeds',
    'url': 'https://sonata-project.org/blog/archive.rss'
}) }}

Render a block by providing the block’s cache options:

1
2
3
4
{{ sonata_block_render(block, {
    'use_cache': use_cache,
    'extra_cache_key': extra_cache_key
}) }}

Render a block by calling an event:

1
2
3
{{ sonata_block_render_event('node.comment', {
    'target': post
}) }}

Note

Review the Events section for more information: Events

Rendering a block related to javascripts and stylesheets for the current page implies the helpers to be called at the end of the page:

1
2
{{ sonata_block_include_stylesheets('screen', app.request.basePath) }}
{{ sonata_block_include_javascripts('screen', app.request.basePath) }}

The app.request.basePath must be provided if your application is stored in a sub-folder.