user2252786
user2252786

Reputation: 1635

Logic names - current controller's name

This is some part of my code:

class FooController extends Controller {
   public function defaultAction() {
      return $this->render('myBarBundle:Foo:someview.html.twig');
   }
}

So every time I use rendering function I need to use current bundle and controller's name. And I was just curious, is there something that could shorten up this syntax a bit and kind of load current bundle/controller? So that I need to type just a view file name?

Upvotes: 1

Views: 124

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52493

Yes there is the @Template annotation provided by SensioFrameworkExtraBundle which comes with symfony2 standard edition.

// YourBundle/Controller/YourController.php

/**
 * @Template
 */
public function defaultAction(Post $post)
{
}

// this will automatically render 'YourBundle:YourController:default.html.twig

In this case all method arguments are automatically passed to the template if the method returns null and no vars attribute is defined!

... Or if you want to specify a specific Template, use:

/**
 * @Template ("SensioBlogBundle:Post:show")
 */

... you might aswell have a look at the @ParamConverter annotation.

Attention

If you just want to serve a static template ( like in your example ) without having to write a controller/action you can use the provided FrameworkBundle:Template:template controller. Read more about it here.

acme_privacy:
    path: /privacy
    defaults:
        _controller: FrameworkBundle:Template:template
        template: 'AcmeBundle:Static:privacy.html.twig'

Upvotes: 1

Related Questions