How to render a Template without a custom Controller

Version: 2.0
Edit this page

Warning: You are browsing the documentation for Symfony 2.0, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

How to render a Template without a custom Controller

Usually, when you need to create a page, you need to create a controller and render a template from within that controller. But if you're rendering a simple template that doesn't need any data passed into it, you can avoid creating the controller entirely, by using the built-in FrameworkBundle:Template:template controller.

For example, suppose you want to render a AcmeBundle:Static:privacy.html.twig template, which doesn't require that any variables are passed to it. You can do this without creating a controller:

  • YAML
  • XML
  • PHP
1
2
3
4
5
acme_privacy:
    pattern: /privacy
    defaults:
        _controller: FrameworkBundle:Template:template
        template: 'AcmeBundle:Static:privacy.html.twig'

The FrameworkBundle:Template:template controller will simply render whatever template you've passed as the template default value.

You can of course also use this trick when rendering embedded controllers from within a template. But since the purpose of rendering a controller from within a template is typically to prepare some data in a custom controller, this probably isn't useful, except to easily cache static partials, a feature which will become available in Symfony 2.2.

  • Twig
  • PHP
1
{% render url('acme_privacy') %}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.