Re: [RFC] Better string interpolation

From: Date: Sun, 16 Aug 2020 21:33:54 +0000
Subject: Re: [RFC] Better string interpolation
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-111564@lists.php.net to get a copy of this message
Hi Ilija, On 14/08/2020 17:14, Ilija Tovilo wrote:
I've been thinking about ways to improve string interpolation. String interpolation in PHP is currently pretty limited to say the least.
Thanks for putting together some thoughts on this. My instinct is that rather than having a whole new syntax that's basically a tweak on "${expression}", we could take the opportunity to bring more features into the mix. If you think about it, string interpolation is basically an inline template syntax, so a lot of the features that are useful in template engines would be useful there. Specifically: 1) modifiers; e.g. sprintf-style, f"Measure #0.2f{$value}" or template-style, f"Hello #{$name|escape}" 2) custom rules, like JavaScript's tagged templates [1], e.g. html"Hello #{$name}" == "Hello " . htmlspecialchars($name) Put these together, and you can have variables escaped by default, and opted out via a modifier: $tag = 'h1'; echo html"<#raw{$tag}>Hello #{$_GET['name']}</#raw{$tag}>"; If we took JS tagged templates as the inspiration, that would de-sugar to something like: echo render_html_string(
    stringParts: [ '<', '>Hello ', '</', '>' ],
    expressions: [ $tag, $_GET['name'], $tag ],
    modifiers: [ ['raw'], [], ['raw'] ]
); It will take a while to figure out the details, but I think a powerful feature like this is more worthy of adding new syntax. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals Regards, -- Rowan Tommins (né Collins) [IMSoP]

« previous php.internals (#111564) next »