Clément Andraud
Clément Andraud

Reputation: 9279

Twig get url parameter with []

I have an url like : MYURL?filter[_per_page]=25&filter[name][value]=hello

How can i get these parameters with twig ?

I'm trying {{ app.request.get('filter[_per_page]') }} but it's always empty...

Thanks !

Edit : I'm in javascript an i want to assign this result to a javascript variable like : var param = "{{ app.request.get('filter[_per_page]') }}";

Upvotes: 22

Views: 57032

Answers (4)

Rasta
Rasta

Reputation: 11

To access to array of query params you can use:

app.request.attributes.get('_route_params');

You can see different solutions on the documentation.

Upvotes: 1

daday
daday

Reputation: 1

I found a solution like this :

app.request.attributes.get('request').query.get('param_name')

Upvotes: 0

Matteo
Matteo

Reputation: 39470

You must manage as an array accessing to the filter element as:

{{ app.request.get('filter')['_per_page'] }}

(This time I try before posting...)

Upvotes: 37

Jovan Perovic
Jovan Perovic

Reputation: 20201

You've almost got it.

app object is GlobalVariables instance. When you say app.request, getRequest() is being invoked and returns an instance of standard Request object.

Now if you look at Request::get() (link) there is:

get(string $key, mixed $default = null, bool $deep = false)

I think what you need to do is this:

{{ app.request.get('filter[_per_page]', NULL, true) }}

Where NULL is default value and true means deep traversal of Request object.

Upvotes: 8

Related Questions