Twig icon indicating copy to clipboard operation
Twig copied to clipboard

Twig strings don't support PHP string escape sequences

Open GPHemsley opened this issue 3 years ago • 1 comments

I'm trying to insert a non-breaking space (NBSP) into a string in a Twig template, but all the ways I've thought to try haven't worked.

And upon further research it seems that Twig strings do not support the full gamut of escape sequences that PHP supports: https://www.php.net/manual/en/language.types.string.php

For example, this code:

{{ "\240\xA0\u{00A0} " }}

produces:

��u{00A0} 

instead of:

    

I've elaborated some here: https://twigfiddle.com/q41fr9

As such, I have two questions: (1) Is this expected behavior? (2) Is there a way to do what I want to do that I'm missing?

GPHemsley avatar Dec 19 '21 19:12 GPHemsley

Twig relies on stripcslashes which indeed does not cover the PHP escape sequences but only the C ones.

stof avatar Dec 20 '21 10:12 stof