Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access Route parameter in twig template directly?

Tags:

twig

symfony

A view element on my page depends on a route parameter and will render something if a route parameter is present. Is it possible to access Route parameter in twig template directly?

For example:

TestBundle_testroute:     pattern:  /{name}     defaults: { _controller: TestBundle:Default:test, name: defaultname } 

I would like to be directly able to access “name” route parameter in Twig. Something like:

{{ routing.name }} 
like image 533
DavidW Avatar asked Dec 25 '11 09:12

DavidW


People also ask

What is Route params?

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req. params object, with the name of the route parameter specified in the path as their respective keys.

How do I find the URL of a twig?

You can get the current URL in Twig/Silex 2 like this: global. request. attributes. get('_route') .


2 Answers

You can achieve it like this:

{{ app.request.get('name') }} 
like image 148
Elnur Abdurrakhimov Avatar answered Sep 29 '22 15:09

Elnur Abdurrakhimov


I am also having the same problem. To fix this issue I fist dumped the request object and go through the attributes. In attributes you can see all the available properties associated with the request which can be accessed by twig. For example

app.request.attributes('_route'); //gives you route name app.request.attributes('slug'); //gives you path variable with in the controller with the name 'slug' 
like image 42
msucil Avatar answered Sep 29 '22 15:09

msucil