Syntax 如何在细枝中连接字符串
有人知道如何在细枝中连接字符串吗?我想做一些类似的事情:Syntax 如何在细枝中连接字符串,syntax,twig,string-concatenation,templating,Syntax,Twig,String Concatenation,Templating,有人知道如何在细枝中连接字符串吗?我想做一些类似的事情: {{ concat('http://', app.request.host) }} 这应该很好: {{ 'http://' ~ app.request.host }} 要在同一标记中添加类似“trans”的筛选器,请使用 {{ ('http://' ~ app.request.host) | trans }} 同样,您也可以使用,这确实需要双引号字符串: {{ "http://#{app.request.host}" }} 在这种
{{ concat('http://', app.request.host) }}
这应该很好:
{{ 'http://' ~ app.request.host }}
要在同一标记中添加类似“trans”的筛选器,请使用
{{ ('http://' ~ app.request.host) | trans }}
同样,您也可以使用,这确实需要双引号字符串:
{{ "http://#{app.request.host}" }}
在这种情况下,如果要输出纯文本和变量,可以这样做:
http://{{ app.request.host }}
如果你想连接一些变量,alessandro1997的解决方案会更好
{{ ['foo', 'bar'|capitalize]|join }}
正如您所看到的,它可以与过滤器和函数一起工作,而无需在单独的行上使用
set
。在Symfony中,您可以将其用于协议和主机:
{{ app.request.schemeAndHttpHost }}
尽管@alessandro1997给出了一个关于连接的完美答案。每当你需要使用带有连接字符串(或基本数学运算)的过滤器时,你应该用()来包装它。例如:
{('http://'~app.request.host)| url_encode}}
要混合字符串、变量和翻译,我只需执行以下操作:
{% 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>
' %}
{%set添加链接=”
' %}
尽管所有的东西都混在一起了,但它还是像一个符咒一样工作。您正在寻找的操作员是Tilde(~),就像Alessandro所说的,它在文档中: ~:将所有操作数转换为字符串并连接它们。{{“你好 “~name~”!“}}将返回(假设名称为‘John’)你好,John!—— 下面是一个例子:
细枝中还有一个鲜为人知的特征: “{…}}”-分隔符也可以在字符串中使用:
"http://{{ app.request.host }}"
您可以像使用
{{foo~'inline string'~bar.fieldName}}
但您也可以创建自己的concat
函数,像在问题中一样使用它:{{concat('http://',app.request.host)}
:
在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';
}
}
快速回答(TL;DR)
- 细枝字符串连接也可以使用
format()
过滤器完成
详细答案
上下文
- 小枝2.x
- 字符串构建和连接
问题
- 场景:DeveloperGailSim希望在Twig中进行字符串连接
- 此线程中的其他答案已针对concat运算符
- 这个答案集中在更具表现力的
格式
过滤器上
解决方案
- 另一种方法是使用
格式
过滤器
format
过滤器的工作原理与其他编程语言中的sprintf
函数类似
- 对于更复杂的字符串,
格式
筛选器可能比~运算符简单
例00
- 示例00字符串concat bare
{{ "%s%s%s!"|format('alpha','bravo','charlie') }}
--- result --
alphabravocharlie!
另见
这对我不起作用,因为我必须用另一个过滤器对整个字符串进行url\u编码…谢谢你的回答。但是| trans filter似乎不起作用(例如:{{'test|}~name{trans}}}不会翻译我的项目。你知道怎么做吗?thx!是的,你必须创建一个变量来保存连接的字符串。例如:{%set foo='http://'~app.request.host%}
,然后你可以执行:{foo{trans
.Translation在一行:{{('test{name){trans}谢谢。所以问题是过滤器的优先级高于串联运算符。这对我来说很有效,将字符串作为一个参数传递到函数中,变量上的过滤器和函数标记本身:{form u}open('admin/files/?path='~file_path | urlencode)| raw}}
不需要额外的变量。非常有用,谢谢。我需要连接变量以用作转换键。功能很好。请注意,只有双引号字符串!
<?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';
}
}
services:
app.twig_extension:
class: AppBundle\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
{{ "%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!
{{ "The %04d in %04d falls mainly on the %s!"|format(2,3,'tree') }}
--- result --
The 0002 in 0003 falls mainly on the tree!