Of course it is. tuxedo25
explained it how here: How to get current route in Symfony 2?
Basically:
$routeName = $request->get('_route');
if ( $routeName == "item_add"){
// render template "add.html.twig"
}else{
// render template "edit.html.twig"
}
Hope this helps...
Idea #1:
If you want to use @Template
you could still keep the annotation as usual but you would:
return array(
'route_name' => $routeName,
// some other data
);
Then:
item.html.twig
{% include routeName == 'item_add' ? 'item_add.html.twig' : 'item_edit.html.twig' %}
Nevertheless, while this solves the issue, I feel like it's very cumbersome. Personally I would never go this way.
Idea #2:
This idea takes whole different angle. While it does not use @Template
it minimizes the possibility of error:
/**
* @Route("/add", name="item_add")
* @Route("/edit/{id}", name="item_edit")
*/
public function editAction(Request $request, Item $item = null){
$routeName = $request->get('_route');
# Your controller's logic here
return $this->renderView(sprintf('VendorNamespaceBundle:ControllerClass:%s.html.twig', $routeName), aray(... your data here ... ));
}