首页 文章

如何在twig中连接字符串

提问于
浏览
366

任何人都知道如何连接树枝中的字符串?我想做的事情如下:

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

11 回答

  • 735

    每当你需要使用带有连接字符串的过滤器(或基本的数学运算)时,你应该用()包装它 . 例如 . :

    {{ ('http://' ~ app.request.host) | url_encode }}

  • 11

    在Symfony中,您可以将其用于协议和主机:

    {{ app.request.schemeAndHttpHost }}
    

    虽然@ alessandro1997给出了关于连接的完美答案 .

  • 10

    Twig中一个鲜为人知的功能是string interpolation

    {{ "http://#{app.request.host}" }}
    
  • -1
    {{ ['foo', 'bar'|capitalize]|join }}
    

    正如您所看到的,这适用于过滤器和函数,而无需在单独的行上使用 set .

  • 1

    这应该工作正常:

    {{ 'http://' ~ app.request.host }}
    

    要添加过滤器 - 比如'trans' - 在同一个标签中使用

    {{ ('http://' ~ app.request.host) | trans }}
    

    作为Adam Elsodaney points out,您也可以使用string interpolation,这需要双引号字符串:

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

    要混合字符串,变量和翻译,我只需执行以下操作:

    {% 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>
        ' %}
    

    尽管一切都在混乱,但它的作用就像一个魅力 .

  • 0

    快速回答(TL; DR)

    • 也可以使用 format() 过滤器完成Twig字符串连接

    详细解答

    上下文

    • Twig 2.x

    • 字符串构建和连接

    问题

    • Scenario: DeveloperGailSim希望在Twig中进行字符串连接

    • 此线程中的其他答案已经解决了concat运算符

    • 这个答案集中在更具表现力的 format 过滤器上

    解决方案

    • 替代方法是使用 format 过滤器

    • format 过滤器与其他编程语言中的 sprintf 函数类似

    • 对于更复杂的字符串, format 过滤器可能不如〜运算符麻烦

    Example00

    • example00字符串concat裸
    {{ "%s%s%s!"|format('alpha','bravo','charlie') }}
    
    --- result --
    
    alphabravocharlie!
    

    Example01

    • example01字符串连接,带有插入文本
    {{ "The %s in %s falls mainly on the %s!"|format('alpha','bravo','charlie') }}
    
    --- result --
    
    The alpha in bravo falls mainly on the charlie!
    

    例02

    • example02字符串concat与数字格式

    • 遵循与其他语言中的 sprintf 相同的语法

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

    另见

  • 4

    “{}” - 分隔符也可以在字符串中使用:

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

    您正在寻找的运营商是Tilde(〜),就像Alessandro说的那样,这里是文档:

    〜:将所有操作数转换为字符串并连接它们 . {{“你好”〜名字〜“!”会返回(假设名字是'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 #}
    
  • 6

    在这种情况下,您想要输出纯文本和变量,您可以这样做:

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

    如果你想连接一些变量,alessandro1997的解决方案会好得多 .

  • 21

    您可以像 {{ 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 }
    

相关问题