How to render a Template without a custom Controller
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
1 2 3 4 5
acme_privacy: pattern: /privacy defaults: _controller: FrameworkBundle:Template:template template: 'AcmeBundle:Static:privacy.html.twig'
- XML
1 2 3 4 5 6 7 8 9 10 11
<?xml version="1.0" encoding="UTF-8" ?> <routes xmlns="http://symfony.com/schema/routing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> <route id="acme_privacy" pattern="/privacy"> <default key="_controller">FrameworkBundle:Template:template</default> <default key="template">AcmeBundle:Static:privacy.html.twig</default> </route> </routes>
- PHP
1 2 3 4 5 6 7 8 9 10
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $collection = new RouteCollection(); $collection->add('acme_privacy', new Route('/privacy', array( '_controller' => 'FrameworkBundle:Template:template', 'template' => 'AcmeBundle:Static:privacy.html.twig', ))); return $collection;
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
1
{% render url('acme_privacy') %}
- PHP
1 2 3
<?php echo $view['actions']->render( $view['router']->generate('acme_privacy', array(), true) ) ?>
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.