任何人都知道如何在树枝中连接字符串?我想做点什么:
{{ concat('http://', app.request.host) }}
答案 0 :(得分:777)
这应该可以正常工作:
{{ 'http://' ~ app.request.host }}
要在同一标记中添加过滤器(如“trans”),请使用
{{ ('http://' ~ app.request.host) | trans }}
作为Adam Elsodaney points out,您还可以使用string interpolation,这需要双引号字符串:
{{ "http://#{app.request.host}" }}
答案 1 :(得分:78)
Twig中的一个鲜为人知的功能是string interpolation:
{{ "http://#{app.request.host}" }}
答案 2 :(得分:23)
你正在寻找的运营商是Tilde(〜),就像亚历山德罗说的那样,这里有文件:
〜:将所有操作数转换为字符串并连接它们。 {{ “你好 “~name~”!“}}会返回(假设名字是'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 #}
答案 3 :(得分:21)
在这种情况下,您想要输出纯文本和变量,您可以这样做:
http://{{ app.request.host }}
如果你想连接一些变量,alessandro1997的解决方案会好得多。
答案 4 :(得分:12)
{{ ['foo', 'bar'|capitalize]|join }}
正如您所看到的,这适用于过滤器和函数,而无需在单独的行上使用set
。
答案 5 :(得分:11)
每当你需要使用带有连接字符串的过滤器(或基本的数学运算)时,你应该用()包装它。例如:
{{ ('http://' ~ app.request.host) | url_encode }}
答案 6 :(得分:6)
在Symfony中,您可以将其用于协议和主机:
{{ app.request.schemeAndHttpHost }}
尽管@ alessandro1997给出了关于连接的完美答案。
答案 7 :(得分:4)
您可以~
使用{{ 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';
}
}
在app/config/services.yml
:
services:
app.twig_extension:
class: AppBundle\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
答案 8 :(得分:1)
要混合字符串,变量和翻译,我只需执行以下操作:
{% 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>
' %}
尽管一切都在混乱,但它的作用就像一个魅力。
答案 9 :(得分:0)
format()
过滤器format
过滤器format
过滤器format
过滤器的工作方式与其他编程语言中的sprintf
功能类似format
过滤器可能不如〜运算符麻烦example00 string concat bare
{{ "%s%s%s!"|format('alpha','bravo','charlie') }} --- result -- alphabravocharlie!
example01 string concat with intervening text
{{ "The %s in %s falls mainly on the %s!"|format('alpha','bravo','charlie') }} --- result -- The alpha in bravo falls mainly on the charlie!
遵循与其他语言中的sprintf
相同的语法
{{ "The %04d in %04d falls mainly on the %s!"|format(2,3,'tree') }} --- result -- The 0002 in 0003 falls mainly on the tree!
答案 10 :(得分:-1)
“{{...}}” - 分隔符也可以在字符串中使用:
Private Sub ListBox1_Change()
Dim counter1 As Integer, counter2 As Integer, counter3 As Integer, counter4 As Integer, counter5 As Integer, counter6 As Integer, selectedCount As Integer
selectedCount = 0
For counter = 1 To FirstQuestion.ListBox1.ListCount Step 1
If FirstQuestion.ListBox1.Selected(counter - 1) = True Then
selectedCount = selectedCount + 1
If selectedCount >= 6 Then
FirstQuestion.ListBox1.Selected(FirstQuestion.ListBox1.ListIndex) = False
MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
Exit Sub
End If
End If
Next counter
For counter2 = 1 To SecondQuestion.ListBox1.ListCount Step 1
If SecondQuestion.ListBox1.Selected(counter2 - 1) = True Then
selectedCount = selectedCount + 1
If selectedCount >= 6 Then
SecondQuestion.ListBox1.Selected(SecondQuestion.ListBox1.ListIndex) = False
MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
Exit Sub
End If
End If
Next counter2
For counter3 = 1 To ThirdQuestion.ListBox1.ListCount Step 1
If ThirdQuestion.ListBox1.Selected(counter3 - 1) = True Then
selectedCount = selectedCount + 1
If selectedCount >= 6 Then
ThirdQuestion.ListBox1.Selected(ThirdQuestion.ListBox1.ListIndex) = False
MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
Exit Sub
End If
End If
Next counter3
For counter4 = 1 To FourthQuestion.ListBox1.ListCount Step 1
If FourthQuestion.ListBox1.Selected(counter4 - 1) = True Then
selectedCount = selectedCount + 1
If selectedCount >= 6 Then
FourthQuestion.ListBox1.Selected(FourthQuestion.ListBox1.ListIndex) = False
MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
Exit Sub
End If
End If
Next counter4
For counter5 = 1 To FifthQuestion.ListBox1.ListCount Step 1
If FifthQuestion.ListBox1.Selected(counter5 - 1) = True Then
selectedCount = selectedCount + 1
If selectedCount >= 6 Then
FifthQuestion.ListBox1.Selected(FifthQuestion.ListBox1.ListIndex) = False
MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
Exit Sub
End If
End If
Next counter5
For counter6 = 1 To SixthQuestion.ListBox1.ListCount Step 1
If SixthQuestion.ListBox1.Selected(counter6 - 1) = True Then
selectedCount = selectedCount + 1
If selectedCount >= 6 Then
SixthQuestion.ListBox1.Selected(SixthQuestion.ListBox1.ListIndex) = False
MsgBox "Pick 5 questions only", vbInformation + vbOKOnly, "Retry:"
Exit Sub
End If
End If
Next counter6
End Sub