“Begins with” in Twig template

后端 未结 4 1383
半阙折子戏
半阙折子戏 2021-02-05 01:35

I have a twig template where I would like to test if an item begins with a certain value

{% if item.ContentTypeId == \'0x0120\' %}
    

        
4条回答
  •  不要未来只要你来
    2021-02-05 01:59

    You can do that directly in Twig now:

    {% if 'World' starts with 'F' %}
    {% endif %}
    

    "Ends with" is also supported:

    {% if 'Hello' ends with 'n' %}
    {% endif %}
    

    Other handy keywords also exist:

    Complex string comparisons:

    {% if phone matches '{^[\\d\\.]+$}' %} {% endif %}
    

    (Note: double backslashes are converted to one backslash by twig)

    String contains:

    {{ 'cd' in 'abcde' }}
    {{ 1 in [1, 2, 3] }}
    

    See more information here: http://twig.sensiolabs.org/doc/templates.html#comparisons

提交回复
热议问题