Twig"修剪"不按预期工作

Tay*_*orR 0 php symfony twig

使用此Twig模板:

{% for line in lines%}
    {{line}} after trim('.php') is: {{line|trim('.php')}}<br>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

使用Silex中的控制器呈现上述模板:

$app->get('/try', function () use ($app)
{
    $lines=[];
    $lines[]='123.php';
    $lines[]='news.php';
    $lines[]='map.php';

    return $app['twig']->render('try.html.twig', ['lines'=>$lines]);
}
    );
Run Code Online (Sandbox Code Playgroud)

我收到以下输出:

123.php after trim('.php') is: 123
news.php after trim('.php') is: news
map.php after trim('.php') is: ma
Run Code Online (Sandbox Code Playgroud)

注意最后一个修剪:map.php应该成为map但现在ma.

sja*_*agr 5

根据其他列出的答案,你错误地解释了Twig的trim功能是如何工作的(它将参数用作字符映射).

您可能正在寻找的是replace过滤器:

{% for line in lines %}
    {{ line }} after replace({'.php': ''}) is: {{ line|replace({'.php': ''}) }}<br>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)