dongqixian8474 2013-02-22 17:18
浏览 31
已采纳

preg_match在树枝上

First of all, I know that the logic should be in the controller and not in the view and I keep it that way.

But in this particular situation I need to use preg_match within a ternary operation to set the css class of a div.

Example:

{% for list in lists %}
    <div class="{{ (preg_match(list.a, b))>0 ? something : else }}"...>...</div>
{% endfor %}

How can I achieve the (preg_match(list.a,b))>0 condition in twig?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • duanchao9559 2013-02-22 18:52
    关注

    You can't use the preg_match directly but there are ways to accomplish it

    1) if list is an entity add a method matches {{ (list.matches(b)) ? something : else }}

    2) you could create a custom twig extension function that uses preg_match internally http://symfony.com/doc/master/cookbook/templating/twig_extension.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址