pug-php / pug

Pug template engine for PHP
https://www.phug-lang.com
MIT License
387 stars 42 forks source link

Interpolation does not support null coalescing operator #235

Closed char101 closed 4 years ago

char101 commented 4 years ago

Hello,

With the variables set as ['a' => 10]

I encountered an issue with the following code:

p #{ a ?? 'a' }

I expected to get:

<p>10</p>

But I actually get:

Use of undefined constant a - assumed 'a' (this will throw an Error in a future version of PHP) on line 2, offset 4

Thanks!

kylekatarnls commented 4 years ago

Hello, ?? does not exist in JS.

As your expression is in JS-style, you should use:

p #{ a || 'a' }

Or to match exactly the behavior of ??:

p #{ a === null ? 'a' : 10 }

Else in PHP-style:

p #{ $a ?? 'a' }

Thanks.

char101 commented 4 years ago

Hi, thanks for the prompt answer. Since #{ a } works I was assuming it was php style expression without $ prefix for variables (ala Twig) but apparently it was the library doing magic tricks 😁 .

kylekatarnls commented 4 years ago

What exactly happens is a ?? 'a' failed to be parsed as a JS expression so it's evaluated as is by PHP but a constant does not exist, that's why other expression above work.

Note: I discovered the meaning of "ala" here https://www.yourdictionary.com/ala For the side story, I'm French and "ala" does not exist in French, we would say "à la Twig" as a familiar short-hand for "à la manière de Twig" the definition given by this dictionary is right for "prepared in a certain way" ("à la" may match this part of the definition), but as for "or for a particular" and the "chicken ala king" example, that definitely does not come from the French language. ^^

char101 commented 4 years ago

Thanks for the explanation. I wasn't familiar yet with the ecosystem for the library. So pug-php by default renders expression using js style with js-phpize.

As a side note, I am Indonesian, and in this language, ala means exactly like a la in French, which is in the style of, which is why I confuse the two 😁 .