Silex - How to process template includes separately

129 views Asked by At

I've got a Twig base template like so:

{% include "./partials/navigation.html" %}

<!-- Main Wrapper -->
<div id="wrapper">
    {% block content %}{% endblock content %}
</div>

I also have a route controller which is outputting the response content to the page using twig:

return template->render('path/to/teplate', args());

where args[] array is all the data needed for this bit: (different on every page)

{% block content %}{% endblock content %}

However my sidebar is being built separately through a menu builder class, and now it needs to render the results of building the menu to my template and populating ./partials/navigation.html.

An easy solution for me is to just append the results of the Menu Builder to the returned response of every controller (I can use a base controller class to do this as this menu appears on every page). However something about this feels unclean as if I have to render the same header/footer every time as well I'll have to be append all 3 outputs to the response. (maybe that is okay?)

Is there a better way of rendering several includes worth of content which each need their own DB lookups and other server-side logic?

1

There are 1 answers

3
John Cartwright On BEST ANSWER

Yes. They are called sub requests. Read the documentation here.