pug-php / pug

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

js equivalent of isset() #107

Closed webdevian closed 7 years ago

webdevian commented 7 years ago

Using 'expressionLanguage' => 'js', I have the following code:

if url
  // Do something with url
else
  // Do something else

It compiles to

if($url)

If url is not defined in the locals I get a php warning Undefined variable: url.

I would expect this if statement to evaluate the undefined url variable as falsey, and not throw a warning

Or is there a different way I can write the if statement so that is compiles to something like if(isset($url) || $url)?

kylekatarnls commented 7 years ago

Hi,

PHP functions are allowed in JS mode:

if isset(url) && url
  p ok
else
  p ko

In PHP mode, just add the $:

if isset($url) && $url
  p ok
else
  p ko

Next version will follow the tale-jade way and append issetautomatically.

kylekatarnls commented 6 years ago

isset is no longer necessary in pug-php 3 thanks to the check feature: https://phug.selfbuild.fr/#uncheck-check-code Hope you will enjoy this new version.