Twig Append Content to Block Twig Append Content to Block symfony symfony

Twig Append Content to Block


This should do the trick:

{% block css %}    {{ parent() }}    div a { color: #777; }{% endblock css %}{% block content %}    <div><a>myWidget content here...</a></div>{% endblock content %}


Shortcut to append/prepend content to blocks with few content, e.g. a pagetitle

base.html.twig

...<title>{% block title %}MyApp{% endblock %}</title>...

template extending base layout

{% extends '::base.html.twig' %}{% block title 'Page1 - '~parent() %} {# prepend #}{% block title parent()~' - Page1' %} {# append #}


Calling parent() in the child template works, but each child must explicitly accept inheritance from the parent. You can also choose to enforce this inheritance by using a sub-block instead.

inner.html.twig

{% block css %}    a { color: #fff; }    body { background: #f00; }    {% block css_custom %}{% endblock css_custom %}{% endblock css %}

myWidget.html.twig

{% block css_custom %}    div a { color: #777; }{% endblock css_custom %}