pug-php / pug

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

String behavior is confused and inconsistent #90

Closed hax closed 7 years ago

hax commented 8 years ago
p= '$test'
p= "$test"
p= '#{$test}'
p= "#{$test}"
p #{$test}

p(
    data-a='$test'
    data-b="$test"
    data-c='#{$test}'
    data-d="#{$test}"
) test

With data ['test' => 'testing'] and option expressionLang php or auto, the result is:

<p>testing</p>
<p>#{$test}</p>
<p>#testing</p>
<p>testing</p>
<p data-a="$test" data-b="$test" data-c="testing" data-d="testing">test</p>

= output uses PHP strings syntax and ignore the pug interpolations syntax #{expression}. But attribute output does not use PHP strings syntax and process #{expression} which is not very consistent.

With expressionLang js the result is:

<p>testing</p>
<p>#{$test}</p>
<p>#testing</p>
<p>testing</p>
<p data-a="$test" data-b="$test"
// Notice: Undefined variable: testing in pug.stream

= output still uses PHP strings syntax which confused, and there are bug in process #{expression} in attributes.

kylekatarnls commented 8 years ago

Hi, unfortunatly, js expression is not yet implemented everywhere. For php/auto, only p= '$test' seems wrong to me, it should render:

<p>$test</p>

All other codes seems right to me.

I'll try to fix the first one if I can.

hax commented 8 years ago

Well, the problem here is if we treat strings as PHP syntax (just like pugjs drop '#{...}' in strings), there will be many compatibility problems for old php-jade users like our company. Maybe the smooth path is introducing an separate option old-string-syntax.

kylekatarnls commented 8 years ago

Hi, we will not be able to maintain backward compatibility. The next major version of Pug-php will match the pugjs 2 API and will drop all deprecated features. For example string interpolations in attributes will no longer be available in pugjs 2 so do we in pug-php 3 and the same way, we will filter strings to escape $ in "" to get equivalent results to JS. But we might implement a ES2015 literals equivalent (strings with mini-quotes):

a(href=`/foo/${id}/bar`) #{id}

This syntax will be a job for the js-phpize project.

kylekatarnls commented 7 years ago

Hi, there is a new option now available to use the native pugjs engine (as an alternative if you still have blocking bugs with the PHP engine):

$pug = new Pug(array(
    'pugjs' => true
));
echo $pug->render('
p= '$test'
p= "$test"
p= '$test' + test
p= "$test" + test
p= test
p #{test}

This is a pure wrapper solution with no PHP support in templates (all run with node.js).

kylekatarnls commented 7 years ago

This has changed in 3.0.0-alpha2: In default mode (expressionLanguage => "js" witouht pugsjs option) supposing test variable is set to "foo":

p= "$test"
p= '#{$test}'
p= "#{$test}"
p #{$test}

p(
    data-a='$test'
    data-b="$test"
    data-c='#{$test}'
    data-d="#{$test}"
) test

Will render:

<p>$test</p>
<p>$test</p>
<p>#{$test}</p>
<p>#{$test}</p>
<p>foo</p>
<p data-a="$test" data-b="$test" data-c="#{$test}" data-d="#{$test}">test</p>

In php mode (expressionLanguage => "php"), you will get:

<p>$test</p>
<p>foo</p>
<p>#{$test}</p>
<p>#foo</p>
<p data-a="$test" data-b="foo" data-c="#{$test}" data-d="#foo">test</p>

Please don't hesitate to test the new pug-php 3 and give us your feedback.