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 }}
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.
You can get the current URL in Twig/Silex 2 like this: global. request. attributes. get('_route') .
You can achieve it like this:
{{ app.request.get('name') }}
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With