首页 > 解决方案 > 在带有 HTML 的 TWIG 短格式 if/else 语句中显示变量

问题描述

{{r.status == 'active' or 'scheduled' ? 'The order status is <a href="https://example.com/"> 
{{r.order}} : 'The order status is cancelled'</a>'

当使用上面的行时,它会打印 {{r.order}} 而不是变量中的值。如何打印值?

标签: phphtmltwig

解决方案


您可以将in运算符字符串插值raw过滤器结合使用:

{{r.status in ['active', 'scheduled'] 
  ? "The order status is <a href=\"https://example.com/\">#{r.order}</a>"|raw 
  : 'The order status is cancelled'}}

演示:https ://twigfiddle.com/tj2skl


推荐阅读