Talesoft / tale-jade

A complete and fully-functional implementation of the Jade template language for PHP
http://jade.talesoft.codes
MIT License
88 stars 10 forks source link

Automatic isset-checks behave differently in different cases #126

Open cronfy opened 7 years ago

cronfy commented 7 years ago

Consider this code:

p #{$var}
    $var

Produces:

<p>
  <?=htmlentities(isset($var) ? $var : '', \ENT_QUOTES, 'UTF-8')?>
  <?=htmlentities($var, \ENT_QUOTES, 'UTF-8')?>
</p>

First occurence was checked by isset, and second was not.

  1. Is it possible to configure isset checks to behave the same way through all the code?
  2. It is possible to disable isset checks completely?
TorbenKoehn commented 7 years ago

It's not possible to disable them completely right now, but you can control them with ?. Writing ?#{$var} will disable it for that interpolation.

http://sandbox.jade.talesoft.codes/id-5853e8edde434.html

It seems I completely forgot about isset checks when using Variable-tokens. You can avoid this by prepending =

Take a look at this:

http://sandbox.jade.talesoft.codes/id-5853e9c11d62a.html

I will not patch this in Tale Jade, as we're currently discussing the automatic isset() checks for our new Phug implementation and we will implement a consistent behavior over there.

cronfy commented 7 years ago

Thanks for advices! I hope in Phug isset checks will be configurable :) BTW, when are you going to release it?

TorbenKoehn commented 7 years ago

We don't know yet, we basically just started and we're working out the structure and guidelines currently. A lot of stuff has already been finished. This time we aim to become one of the bigger, professional template engines ready for production and enterprise usage. Because of this, we take our time, make sure to find stable and convenient structures and write docs and unit tests without an end to them.

Once it's usable, Tale Jade will fade into the background in favor of Phug. There will be extensions that mimic Tale Jade behavior, so you don't have to change a thing in your existing templates (Most of Tale Jade's "Special-Features" will be put into extensions)

cronfy commented 7 years ago

Sounds great! Have a luck with it!

Could you please give a hint where I can disable isset checks for myself in Tale-jade source?

TorbenKoehn commented 7 years ago

My friend, you won't like the answer. The isset checks are implemented very deeply.

https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L762 https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L1559 https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L1602 https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L1664 https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L1694 https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L1727 https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L1857 https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L2056 https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L2219 https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L2288

The only thing you could try to avoid editing the whole compiler is (maybe) changing Compiler->isVariable() to return false all the time. It's what I use to check if isset should be wrapped or not.

https://github.com/Talesoft/tale-jade/blob/master/Compiler.php#L674

Keep in mind that this could break other things, I remember I also used that somewhere in mixin argument checks and stuff.

I wrote Tale Jade with these isset-checks in mind, since I was porting JavaScript templates to PHP and PugJS does something similar with undefined variables. When I was porting I ran into constant E_NOTICE-floods because this, that and these are not defined.

Why do you want to disable them completely?

cronfy commented 7 years ago

Thanks a lot, I'll give it a try!

I prefer to eliminate "undefined variable" errors instead of hiding them. I use Yii2, where any undefined variable throws an exception. I also use Twig, it has strict_variables option, which does the same, and I keep it always enabled.

Anyway, I think that whether to ignore undefined variables or not is a matter of project coding guidelines/taste and should be configurable for template engine.