Anyone knows how to concatenate strings in twig? I want to do something like:
{{ concat('http://', app.request.host) }}
This should work fine:
{{ 'http://' ~ app.request.host }}
To add a filter - like 'trans' - in the same tag use
{{ ('http://' ~ app.request.host) | trans }}
As Adam Elsodaney points out , you can also use string interpolation , this does require double quoted strings:
{{ "http://#{app.request.host}" }}
Twig 中还有一个鲜为人知的特性是字符串插值:
{{ "http://#{app.request.host}" }}
The operator you are looking for is Tilde (~), like Alessandro said, and here it is in the documentation:
~: Converts all operands into strings and concatenates them. {{ "Hello " ~ name ~ "!" }} would return (assuming name is 'John') Hello John!. – http://twig.sensiolabs.org/doc/templates.html#other-operators
And here is an example 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 #}
In this case, where you want to output plain text and a variable, you could do it like this:
http://{{ app.request.host }}
If you want to concatenate some variables, alessandro1997's solution would be much better.
{{ ['foo', 'bar'|capitalize]|join }}
正如您所看到的,这适用于过滤器和函数,而无需在单独的行上使用set
。
Whenever you need to use a filter with a concatenated string (or a basic math operation) you should wrap it with ()'s. Eg.:
{{ ('http://' ~ app.request.host) | url_encode }}
In Symfony you can use this for protocol and host:
{{ app.request.schemeAndHttpHost }}
Though @alessandro1997 gave a perfect answer about concatenation.
You can use ~
like {{ foo ~ 'inline string' ~ bar.fieldName }}
But you can also create your own concat
function to use it like in your question:
{{ concat('http://', app.request.host) }}
:
In 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';
}
}
In app/config/services.yml
:
services:
app.twig_extension:
class: AppBundle\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
format()
filter format
filter which is more expressiveformat
filterformat
filter works like the sprintf
function in other programming languages format
filter may be less cumbersome than the ~ operator for more complex stringsexample00 string concat bare
\n\n{{ "%s%s%s!"|format('alpha','bravo','charlie') }} \n\n--- result --\n\nalphabravocharlie!\n\n
example01 string concat with intervening text
\n\n{{ "The %s in %s falls mainly on the %s!"|format('alpha','bravo','charlie') }} \n\n--- result --\n\nThe alpha in bravo falls mainly on the charlie! \n\n
follows the same syntax as sprintf
in other languages
\n\n{{ "The %04d in %04d falls mainly on the %s!"|format(2,3,'tree') }} \n\n--- result --\n\nThe 0002 in 0003 falls mainly on the tree! \n\n
To mix strings, variables and translations I simply do the following:
{% 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>
' %}
Despite everything being mixed up, it works like a charm.
"{{ ... }}" 分隔符也可以在字符串中使用:
"http://{{ app.request.host }}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.