简体   繁体   English

Jtwig获取参数显示

[英]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. 我有一些获取参数,例如?perpage = 10&category = all&sort = relevance我想在我的Jtwig代码中阅读此参数并打印出来。 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. 并在这种情况下应显示“ all”,但是这两个请求都在我的机器上返回空字符串。

Sources: Twig - Get URL for canonical tag , https://github.com/symfony/symfony/issues/7960 . 来源: Twig-获取规范标签的URLhttps://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. 然后,您只需要使用Java Servlet API即可获取所需的数据。 In this case, it would be something like: 在这种情况下,它将类似于:

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

JTwig pages implicitly comes with a HttpServletRequest object - request. JTwig页面隐式带有HttpServletRequest对象-请求。 All you need to is to invoke the request object using the key "request" then access every public method of HttpServletRequest using dot(.) notation. 您所需要做的就是使用键“ request”调用请求对象,然后使用dot(。)表示法访问HttpServletRequest的每个公共方法。 eg 例如

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

You can also access your session variable in this manner eg 您也可以通过这种方式访问​​会话变量,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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