[英]twig:concatenate two strings in a trinary inside HTML attribute
<a class="menu-item" href="{{(haveSubMenu == 'yes')?'#':(item.url|e ~ (item.hash ? item.hash|e : ''))}}"></a>
What I wanna do is simple, if the menu-item has submenu, then its href should be #
, if not, then get its URL. 我想做的很简单,如果菜单项具有子菜单,则其href应该是#
,如果没有,则获取其URL。 But the above code doesn't work, because when ~
convert its operands to string, it converts all &
s in item.url
to &
但是上面的代码不起作用,因为当~
将其操作数转换为字符串时,它将item.url
所有&
s转换为&
, which breaks the link. ,这会断开链接。
So what is the correct way to write this? 那么写这个的正确方法是什么?
Try disabling autoscape to all the code with the autoescape tag . 尝试使用autoescape标签对所有代码禁用autoscape。
So with this code: 因此,使用以下代码:
{% autoescape false %}
Everything will be outputted as is in this block
<a class="menu-item" href="{{(haveSubMenu == 'yes')?'#':(item.url|e ~ (item.hash ? item.hash : ''))}}"></a>
{% endautoescape %}
See the working (and running code with example) in this twigfiddle URL . 请参阅此twigfiddle URL中的工作代码(以及带有示例的运行代码)。
If this don't work as you expected feel free to specific some testing data. 如果这不符合您的预期,请随意提供一些测试数据。
Hope this help 希望有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.