How to concatenate strings in twig

SyntaxTwigString ConcatenationTemplating

Syntax Problem Overview


Anyone knows how to concatenate strings in twig? I want to do something like:

{{ concat('http://', app.request.host) }}

Syntax Solutions


Solution 1 - Syntax

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}" }}

Solution 2 - Syntax

Also a little known feature in Twig is string interpolation:

{{ "http://#{app.request.host}" }}

Solution 3 - Syntax

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 #}

Solution 4 - Syntax

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.

Solution 5 - Syntax

{{ ['foo', 'bar'|capitalize]|join }}

As you can see this works with filters and functions without needing to use set on a seperate line.

Solution 6 - Syntax

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 }}

Solution 7 - Syntax

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 }

Solution 8 - Syntax

In Symfony you can use this for protocol and host:

{{ app.request.schemeAndHttpHost }}

Though @alessandro1997 gave a perfect answer about concatenation.

Solution 9 - Syntax

Quick Answer (TL;DR)

  • Twig string concatenation may also be done with the format() filter

Detailed Answer

Context
  • Twig 2.x
  • String building and concatenation
Problem
  • Scenario: DeveloperGailSim wishes to do string concatenation in Twig
    • Other answers in this thread already address the concat operator
    • This answer focuses on the format filter which is more expressive
Solution
  • Alternative approach is to use the format filter
  • The format filter works like the sprintf function in other programming languages
  • The format filter may be less cumbersome than the ~ operator for more complex strings
Example00
  • example00 string concat bare

    {{ "%s%s%s!"|format('alpha','bravo','charlie') }}

    --- result --

    alphabravocharlie!

Example01
  • 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!

Example02
  • example02 string concat with numeric formatting

  • follows the same syntax as sprintf in other languages

    {{ "The %04d in %04d falls mainly on the %s!"|format(2,3,'tree') }}

    --- result --

    The 0002 in 0003 falls mainly on the tree!

See also

Solution 10 - Syntax

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.

Solution 11 - Syntax

The "{{ ... }}"-delimiter can also be used within strings:

"http://{{ app.request.host }}"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstoeflnView Question on Stackoverflow
Solution 1 - SyntaxAlessandro DesantisView Answer on Stackoverflow
Solution 2 - SyntaxAdam ElsodaneyView Answer on Stackoverflow
Solution 3 - SyntaxNabil KadimiView Answer on Stackoverflow
Solution 4 - SyntaxalghimoView Answer on Stackoverflow
Solution 5 - SyntaxSimon EpskampView Answer on Stackoverflow
Solution 6 - SyntaxlsouzaView Answer on Stackoverflow
Solution 7 - SyntaxluchaninovView Answer on Stackoverflow
Solution 8 - SyntaxMinrasView Answer on Stackoverflow
Solution 9 - SyntaxdreftymacView Answer on Stackoverflow
Solution 10 - SyntaxGingiView Answer on Stackoverflow
Solution 11 - Syntaxuser2345998View Answer on Stackoverflow