简体   繁体   中英

Jtwig get parameters display

I have got some get parameters as ?perpage=10&category=all&sort=relevance I want to read this params in my Jtwig code and print it. According to documentation and sources I found this should work:

{{ app.request.attributes.get('category') }}

or

{{ app.request.query.get('category') }}

and should print 'all' in that case, but both of these requests return empty string on my machine.

Sources: Twig - Get URL for canonical tag , https://github.com/symfony/symfony/issues/7960 .

Any suggestions or workarounds would be appreciated.

The request object is always added to the model. Then you just need to use the Java Servlet API to get the data you want. In this case, it would be something like:

{{ request.getParameter('category') }}

JTwig pages implicitly comes with a HttpServletRequest object - request. All you need to is to invoke the request object using the key "request" then access every public method of HttpServletRequest using dot(.) notation. eg

{{ request.getParameter("category") }}

You can also access your session variable in this manner eg

{{ request.session.getAttribute("account") }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM