Velocity 中的条件运算符
有没有办法在 Velocity 中进行三元运算符? 这就是我想做的:
#set ($name = ($args.get(0) == "") ? "default" : $args.get(0))
而不是厚重的 if-else
#if ($args.get(0) == "")
#set ($name = "default")
#else
#set ($name = $args.get(0))
#end
有什么想法吗?
Is there a way to do ternary operators in Velocity?
This is what I'd like to do:
#set ($name = ($args.get(0) == "") ? "default" : $args.get(0))
Instead of chunky if-else
#if ($args.get(0) == "")
#set ($name = "default")
#else
#set ($name = $args.get(0))
#end
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据经验和阅读VTL参考,没有办法做到这一点。 如果您有很多这样的作业,也许您可以考虑定义自己的 velocimacro 来尝试避免重复 if else。
例如,如果宏仅打印单个字符串,您可以执行以下操作:
宏调用周围的双引号很重要,因为这意味着 #set 的 RHS 已被解析。
From experience and reading the VTL Reference there is no way to do this. If you had lots of assignments like this maybe you could look at defining your own velocimacro to try and avoid repeating the if else.
For example, if the macro only prints a single string you could do the following:
The double quotes around the macro call are important as that means the RHS of the #set is parsed.
我最终按照你说的做了,马克:
然后我可以这样称呼它:
I ended up doing as you said, Mark:
And then I can call it like so:
根据记录,对于 Velocity 2.1+,您可以提供备用参考值:
For the record, with Velocity 2.1+, you can provide alternate reference values: