The HttpFragmentServiceProvider provides support for the Symfony fragment sub-framework, which allows you to embed fragments of HTML in a template.
/_fragment
by default).1 | $app->register(new Silex\Provider\HttpFragmentServiceProvider());
|
Note
This section assumes that you are using Twig for your templates.
Instead of building a page out of a single request/controller/template, the fragment framework allows you to build a page from several controllers/sub-requests/sub-templates by using fragments.
Including "sub-pages" in the main page can be done with the Twig render()
function:
1 2 3 4 5 | The main page content.
{{ render('/foo') }}
The main page content resumes here.
|
The render()
call is replaced by the content of the /foo
URL
(internally, a sub-request is handled by Silex to render the sub-page).
Instead of making internal sub-requests, you can also use the ESI (the sub-request is handled by a reverse proxy) or the HInclude strategies (the sub-request is handled by a web browser):
1 2 3 4 5 | {{ render(url('route_name')) }}
{{ render_esi(url('route_name')) }}
{{ render_hinclude(url('route_name')) }}
|