@Template

Caution: You are browsing the documentation for Symfony version 2.0 which is not maintained anymore . If some of your projects are still using this version, consider upgrading to Symfony 2.5.

@Template

Usage

The @Template annotation associates a controller with a template name:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * @Template("SensioBlogBundle:Post:show")
 */
public function showAction($id)
{
    // get the Post
    $post = ...;

    return array('post' => $post);
}

When using the @Template annotation, the controller should return an array of parameters to pass to the view instead of a Response object.

Tip

If the action returns a Response object, the @Template annotation is simply ignored.

If the template is named after the controller and action names, which is the case for the above example, you can even omit the annotation value:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
/**
 * @Template
 */
public function showAction($id)
{
    // get the Post
    $post = ...;

    return array('post' => $post);
}

And if the only parameters to pass to the template are method arguments, you can use the vars attribute instead of returning an array. This is very useful in combination with the @ParamConverter annotation:

1
2
3
4
5
6
7
/**
 * @ParamConverter("post", class="SensioBlogBundle:Post")
 * @Template("SensioBlogBundle:Post:show", vars={"post"})
 */
public function showAction(Post $post)
{
}

which, thanks to conventions, is equivalent to the following configuration:

1
2
3
4
5
6
/**
 * @Template(vars={"post"})
 */
public function showAction(Post $post)
{
}

You can make it even more concise as all method arguments are automatically passed to the template if the method returns null and no vars attribute is defined:

1
2
3
4
5
6
/**
 * @Template
 */
public function showAction(Post $post)
{
}

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License .