DavidW
DavidW

Reputation: 5129

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

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 }}

Upvotes: 42

Views: 56375

Answers (2)

msucil
msucil

Reputation: 846

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'

Upvotes: 1

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44841

You can achieve it like this:

{{ app.request.get('name') }}

Upvotes: 109

Related Questions