php Twig 模板中的“以”开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15600626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Begins with" in Twig template
提问by Eric Herlitz
I have a twig template where I would like to test if an item begins with a certain value
我有一个树枝模板,我想在其中测试一个项目是否以某个值开头
{% if item.ContentTypeId == '0x0120' %}
<td><a href='?parentId={{ item.Id }}'>{{ item.BaseName }}</a><br /></td>
{% else %}
<td><a href='?{{ item.UrlPrefix }}'>{{ item.LinkFilename }}</a></td>
{% endif %}
The 0x0120 can look like that or be more complex like this 0x0120D52000D430D2B0D8DD6F4BBB16123680E4F78700654036413B65C740B168E780DA0FB4BX. The only thing I want to do is to ensure that it starts with the 0x0120.
0x0120 可能看起来像这样或更复杂,像这样 0x0120D52000D430D2B0D8DD6F4BBB16123680E4F78700654036413B65C740B168E780DA0FB4BX。我唯一想做的就是确保它以 0x0120 开头。
The ideal solution would be to solve this by using regex but I'm not aware if Twig supports this?
理想的解决方案是使用正则表达式来解决这个问题,但我不知道 Twig 是否支持这个?
Thanks
谢谢
采纳答案by Marek
Yes, Twig supports regular expressions in comparisons: http://twig.sensiolabs.org/doc/templates.html#comparisons
是的,Twig 在比较中支持正则表达式:http: //twig.sensiolabs.org/doc/templates.html#comparisons
In your case it would be:
在您的情况下,它将是:
{% if item.ContentTypeId matches '/^0x0120.*/' %}
...
{% else %}
...
{% endif %}
回答by benske
You can do that directly in Twig now:
你现在可以直接在 Twig 中做到这一点:
{% 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)
(注意:双反斜杠被twig转换为一个反斜杠)
String contains:
字符串包含:
{{ 'cd' in 'abcde' }}
{{ 1 in [1, 2, 3] }}
See more information here: http://twig.sensiolabs.org/doc/templates.html#comparisons
在此处查看更多信息:http: //twig.sensiolabs.org/doc/templates.html#comparisons
回答by Prisoner
回答by ladaghini
You can always make your own filter that performs the necessary comparison.
您始终可以制作自己的过滤器来执行必要的比较。
As per the docs:
根据文档:
When called by Twig, the PHP callable receives the left side of the filter (before the pipe |) as the first argument and the extra arguments passed to the filter (within parentheses ()) as extra arguments.
当被 Twig 调用时,PHP 可调用对象接收过滤器的左侧(管道 | 之前)作为第一个参数,并将传递给过滤器的额外参数(在括号 () 内)作为额外参数。
So here is a modified example.
所以这是一个修改过的例子。
Creating a filter is as simple as associating a name with a PHP callable:
创建过滤器就像将名称与 PHP 可调用项相关联一样简单:
// an anonymous function
$filter = new Twig_SimpleFilter('compareBeginning', function ($longString, $startsWith) {
/* do your work here */
});
Then, add the filter to your Twig environment:
然后,将过滤器添加到您的 Twig 环境中:
$twig = new Twig_Environment($loader);
$twig->addFilter($filter);
And here is how to use it in a template:
以下是如何在模板中使用它:
{% if item.ContentTypeId | compareBeginning('0x0120') == true %}
{# not sure of the precedence of | and == above, may need parentheses #}
I'm not a PHP guy, so I don't know how PHP does regexes, but the anonymous function above is designed to return true if $longStringbegins with $startsWith. I'm sure you'll find that trivial to implement.
我不是 PHP 专家,所以我不知道 PHP 是如何处理正则表达式的,但是上面的匿名函数旨在如果$longString以$startsWith. 我相信你会发现实施起来很简单。

