How to combine two string in twig?

前端 未结 4 2025
粉色の甜心
粉色の甜心 2021-02-01 12:25

I want to do something like this:

{% set c=a+b %}

Where a and b are strings.
How can I do it?

相关标签:
4条回答
  • 2021-02-01 12:50

    You can use:

    {{ "Hello " ~ name ~ "!" }}

    0 讨论(0)
  • 2021-02-01 12:57

    Use the "~" operator. This will concatenate your two strings. The "+" operator cannot be used to combine strings.

    You would get this:

    {% set c=a~b %}
    

    More info: The "+" operator: Adds two objects together (the operands are casted to numbers).

    0 讨论(0)
  • 2021-02-01 12:59

    The way to do it is:

    {% set c = a ~ b %}
    
    0 讨论(0)
  • 2021-02-01 13:01

    A clearer example for the {% block page %}...{% endblock %}:

    {% block page %}
        {% set page = page | merge({
        "title"       : branchName,
        "description" : "This description has "~branchName~" as its title"
        }) %}
        {{ parent() }}
    {% endblock %}
    

    A clearer example for the {% block content %}...{% endblock %}:

    {% block content %}
        This is just a sample string for {{ branchName }} that needs no concatenation
    {% endblock %}
    
    0 讨论(0)
提交回复
热议问题