ircmaxell / prerano

A new language for PHP
Apache License 2.0
65 stars 1 forks source link

Ordinary code in template-strings #3

Open rkrx opened 7 years ago

rkrx commented 7 years ago

Allow ordinary code in template-strings (like in ES6+; incl. nested templates):

`Todays numbers are ${list.map(x => x * 2).join(', ')}`

Nested strings (nested Backticks):

`Todays numbers are ${list.map(x => x * 2).join(`${separator} `)}`

Compex example:

echo `
    <VirtualHost{$ips.map(ip => ` {$ip}:80`)}>
        ServerName {strtolower($serverName)}
        {$includes |> map(include => "Include {$include}\n")}
    </VirtualHost>` |> stripMargin()

This is already possible with ECMAScript:

`<VirtualHost ${def.ips.map(ip => `${ip}:80`).join(" ")}> ...`

No special need for backticks - code in templates could (should?) be possible with double-quote-strings.

ircmaxell commented 7 years ago

Definitely. Haven't started thinking about string interpolation, but definitely like the idea of ruby/es6 expressions.

The one other thing I'm thinking about is native support for Hack's XHP style embedded markup. So that html would be generated inline:

$html = <a href="${$obj->getLink()}">${$obj->body}</a>;

Something along those lines...

rkrx commented 7 years ago

I am not sure about XHP. Would be a nice addition nontheless. But you should postpone XHP until you're sure, you wont need those syntax-structures otherwise. Having both native XML and native JSON would cover my needs a lot.