2016-04-21 16:52
浏览 63


im reading Find substring in the string in TWIG

and on a single item it works fine for me.

<!-- language: lang-html -->
<li {% if 'page' in app.request.get('_route')|lower %}class="active"{% endif %}>
    <a href="{{path('adminPage')}}">
        <i class="fa fa-file-text"></i> <span>{% trans %}pages_text{% endtrans %}</span> <small class="label pull-right bg-{% if nav_options.count_pages > 0 %}primary{% else %}red{% endif %}">{{ nav_options.count_pages }}</small>

now I find myself needing it to happen differently.

how would I do to find if "items in this array" are contained in "this substring"

the example would be something like this

{% if ['str1','str2'] in/contained in app.request.get('_route')|lower %}class="active"{% endif %}

I tried this and does not work.

I also would rather avoid using the "or" operator, if its a requirement so be it, but if it can be avoided, the better.

I even went and attempted a split parameter.

{% if 'page,blog'|split(',') in app.request.get('_route')|lower %}active {% endif %}

no dice!

the code is meant to allow me to have "several possible routes" inside a collapsible menu item, and if any of the possible route names (i have several route names, blog, blogAdd, blogEdit, blogPost etc) contains "blog" (same with page, and many others) the "active" class should be printed.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题