任何人都知道如何连接树枝中的字符串?我想做的事情如下:
{{ concat('http://', app.request.host) }}
每当你需要使用带有连接字符串的过滤器(或基本的数学运算)时,你应该用()包装它 . 例如 . :
{{ ('http://' ~ app.request.host) | url_encode }}
在Symfony中,您可以将其用于协议和主机:
{{ app.request.schemeAndHttpHost }}
虽然@ alessandro1997给出了关于连接的完美答案 .
Twig中一个鲜为人知的功能是string interpolation:
{{ "http://#{app.request.host}" }}
{{ ['foo', 'bar'|capitalize]|join }}
正如您所看到的,这适用于过滤器和函数,而无需在单独的行上使用 set .
set
这应该工作正常:
{{ 'http://' ~ app.request.host }}
要添加过滤器 - 比如'trans' - 在同一个标签中使用
{{ ('http://' ~ app.request.host) | trans }}
作为Adam Elsodaney points out,您也可以使用string interpolation,这需要双引号字符串:
要混合字符串,变量和翻译,我只需执行以下操作:
{% set add_link = ' <a class="btn btn-xs btn-icon-only" title="' ~ 'string.to_be_translated'|trans ~ '" href="' ~ path('acme_myBundle_link',{'link':link.id}) ~ '"> </a> ' %}
尽管一切都在混乱,但它的作用就像一个魅力 .
format()
Twig 2.x
字符串构建和连接
Scenario: DeveloperGailSim希望在Twig中进行字符串连接
此线程中的其他答案已经解决了concat运算符
这个答案集中在更具表现力的 format 过滤器上
format
替代方法是使用 format 过滤器
format 过滤器与其他编程语言中的 sprintf 函数类似
sprintf
对于更复杂的字符串, format 过滤器可能不如〜运算符麻烦
{{ "%s%s%s!"|format('alpha','bravo','charlie') }} --- result -- alphabravocharlie!
{{ "The %s in %s falls mainly on the %s!"|format('alpha','bravo','charlie') }} --- result -- The alpha in bravo falls mainly on the charlie!
example02字符串concat与数字格式
遵循与其他语言中的 sprintf 相同的语法
{{ "The %04d in %04d falls mainly on the %s!"|format(2,3,'tree') }} --- result -- The 0002 in 0003 falls mainly on the tree!
http://twig.sensiolabs.org/doc/2.x/filters/format.html
https://stackoverflow.com/tags/printf/info
“{}” - 分隔符也可以在字符串中使用:
"http://{{ app.request.host }}"
您正在寻找的运营商是Tilde(〜),就像Alessandro说的那样,这里是文档:
〜:将所有操作数转换为字符串并连接它们 . {{“你好”〜名字〜“!”会返回(假设名字是'John')你好约翰! - http://twig.sensiolabs.org/doc/templates.html#other-operators
这是一个例子somewhere else in the docs:
{% set greeting = 'Hello' %} {% set name = 'Fabien' %} {{ greeting ~ name|lower }} {# Hello fabien #} {# use parenthesis to change precedence #} {{ (greeting ~ name)|lower }} {# hello fabien #}
在这种情况下,您想要输出纯文本和变量,您可以这样做:
http://{{ app.request.host }}
如果你想连接一些变量,alessandro1997的解决方案会好得多 .
您可以像 {{ foo ~ 'inline string' ~ bar.fieldName }} 一样使用 ~
{{ foo ~ 'inline string' ~ bar.fieldName }}
~
但您也可以像在问题中一样创建自己的 concat 函数来使用它:{{ concat('http://', app.request.host) }} :
concat
在 src/AppBundle/Twig/AppExtension.php
src/AppBundle/Twig/AppExtension.php
<?php namespace AppBundle\Twig; class AppExtension extends \Twig_Extension { /** * {@inheritdoc} */ public function getFunctions() { return [ new \Twig_SimpleFunction('concat', [$this, 'concat'], ['is_safe' => ['html']]), ]; } public function concat() { return implode('', func_get_args()) } /** * {@inheritdoc} */ public function getName() { return 'app_extension'; } }
在 app/config/services.yml :
app/config/services.yml
services: app.twig_extension: class: AppBundle\Twig\AppExtension public: false tags: - { name: twig.extension }
11 回答
每当你需要使用带有连接字符串的过滤器(或基本的数学运算)时,你应该用()包装它 . 例如 . :
{{ ('http://' ~ app.request.host) | url_encode }}
在Symfony中,您可以将其用于协议和主机:
虽然@ alessandro1997给出了关于连接的完美答案 .
Twig中一个鲜为人知的功能是string interpolation:
正如您所看到的,这适用于过滤器和函数,而无需在单独的行上使用
set
.这应该工作正常:
要添加过滤器 - 比如'trans' - 在同一个标签中使用
作为Adam Elsodaney points out,您也可以使用string interpolation,这需要双引号字符串:
要混合字符串,变量和翻译,我只需执行以下操作:
尽管一切都在混乱,但它的作用就像一个魅力 .
快速回答(TL; DR)
format()
过滤器完成Twig字符串连接详细解答
上下文
Twig 2.x
字符串构建和连接
问题
Scenario: DeveloperGailSim希望在Twig中进行字符串连接
此线程中的其他答案已经解决了concat运算符
这个答案集中在更具表现力的
format
过滤器上解决方案
替代方法是使用
format
过滤器format
过滤器与其他编程语言中的sprintf
函数类似对于更复杂的字符串,
format
过滤器可能不如〜运算符麻烦Example00
Example01
例02
example02字符串concat与数字格式
遵循与其他语言中的
sprintf
相同的语法另见
http://twig.sensiolabs.org/doc/2.x/filters/format.html
https://stackoverflow.com/tags/printf/info
“{}” - 分隔符也可以在字符串中使用:
您正在寻找的运营商是Tilde(〜),就像Alessandro说的那样,这里是文档:
这是一个例子somewhere else in the docs:
在这种情况下,您想要输出纯文本和变量,您可以这样做:
如果你想连接一些变量,alessandro1997的解决方案会好得多 .
您可以像
{{ foo ~ 'inline string' ~ bar.fieldName }}
一样使用~
但您也可以像在问题中一样创建自己的
concat
函数来使用它:{{ concat('http://', app.request.host) }}
:在
src/AppBundle/Twig/AppExtension.php
在
app/config/services.yml
: