Reputation: 10210
I am rendering a Twig
template as below:
$this->render('base.html.twig');
The content (simplified) of this Twig
template looks as below:
{% block headers %}
...
{% endblock %}
{% block pagecontent %}
...
{# I want to include another template (A) here #}
{# I want to include another template (B) here #}
{% endblock %}
{% block footers %}
...
{% endblock %}
I have another Twig
template which I am not rendering, but I want to include in the above template (where I have placed my Twig
comment). The content is as follows:
{% extends '::base' %}
{% block headers %}
{{ parent() }}
{% endblock %}
{% block pagecontent %}
{{ parent() }}
...
{% endblock %}
I want to eventually render several Twig
templates inside of base.html.twig
.
Is what I am attempting to do achievable, and if so, how do I achieve it?
Upvotes: 1
Views: 534
Reputation: 215
base.html.twig
{% block headers %}
...
{% endblock %}
{% block pagecontent %}
...
{# I want to include another template (A) here #}
{# I want to include another template (B) here #}
{% endblock %}
{% block footers %}
...
{% endblock %}
Your controller:
$this->render('base.html.twig');
Normally, $this->render('view.html.twig'); accepts only one twig. If you want to have several templates, you can build it like this:
view.html.twig
{% extends '::base' %}
{% block pagecontent %}
{# Controller function with template 1 #}
{{ render(controller('AppBundle:Article:recentArticles',{ 'max': 3 })) }}
{# Controller with template 2 #}
{{ render(controller('AppBundle:Article:relatedArticles',{ 'max': 4 })) }}
{% endblock %}
ANOTHER POSSIBLE SOLUTION IS : You can break one block into several blocks.
Upvotes: 0
Reputation: 3812
You just need to render child template (the one extending base.html.twig
).
Change in your controller:
$this->render('child_template_extending_base.html.twig');
Replace child_template_extending_base
with your real template name.
You can also embed
another controllers views in your template with this code:
{{ render(controller(
'AppBundle:Article:recentArticles',
{ 'max': 3 }
)) }}
Read more about this feature here: http://symfony.com/doc/current/book/templating.html#embedding-controllers
Upvotes: 3