twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.11k stars 1.24k forks source link

Add a new concatenation operator with space between strings #1476

Closed lexxpavlov closed 10 years ago

lexxpavlov commented 10 years ago

It is often necessary to concat the two strings with a space between them. I propose adding a special operator for this:

{%
  set hello = "Hello"
  set name = "Alice"
  set greet1 = hello ~ ' ' ~ name
  set greet2 = hello @ name
%}
{{ greet1 }} identical to {{ greet2 }}

I have seen such an operator in UnrealScript, where used @ operator for joining with space, so I brought this operator in the example above.

stof commented 10 years ago

I don't think such operator should go in core. It is very specific and could be hard to understand.

Note that nothing prevents you to define this operator in your own project thanks to a Twig extension.

soatok commented 10 years ago

Indeed; also, this would reserve two different ASCII characters for (essentially) the same operation, which might prevent something down the line from using @ for some other purpose.

lexxpavlov commented 10 years ago

Twig is a well sugared language. I think this operator logically fits in the language. And @soatok rightly mentions about two different characters. Maybe use double tilda for this operator. That will be semantically linked with current concat operator.

{% set greet2 = hello ~~ name %}

In my taste, expression ~ ' ' ~ is terrible.

barryvdh commented 10 years ago

I do agree that it is pretty ugly having to do that, and I also need to do that (ie. First name + last name, zip + address etc ) Not sure about technical issues but just ~~ does seem a but prettier.

fabpot commented 10 years ago

I'm also reluctant to add a special operator for this specific use case.