phpconcatenationtwigdelimiter

change Twig concatenation delimiter


twig uses ~ for its concatenation, is there any way to change this to another symbol?

I know there is a way to change other delimiters, like, the blocks, comments, etc, but I didn't find anything on concatenation. So if someone knows, that would be great!


Solution

  • For the delimeters your talking about, the TwigLexer only define this symbols:

    $this->options = array_merge(array(
        'tag_comment' => array('{#', '#}'),
        'tag_block' => array('{%', '%}'),
        'tag_variable' => array('{{', '}}'),
        'whitespace_trim' => '-',
        'interpolation' => array('#{', '}'),
    ), $options);
    

    As @DarkBee mentioned, you could define your own operator using https://twig.symfony.com/doc/2.x/advanced.html#operators

    You can found already defined operators on this php class to help you define your own: vendor/twig/twig/lib/Twig/Extension/Core.php class Twig_Extension_Core::getOperators

    Your operator would be

    class Project_Twig_Extension extends Twig_Extension
    {
        public function getOperators()
        {
            return array(
                array(),
                array(
                   '~' => array('precedence' => 40, 'class' => 'Twig_Node_Expression_Binary_Concat', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
                ),
            );
        }
    
        // ...
    }