phug-php / phug

Phug - The Pug Template Engine for PHP
https://phug.selfbuild.fr
MIT License
62 stars 3 forks source link

Newline after interpolation #64

Closed char101 closed 4 years ago

char101 commented 4 years ago

Hello,

I encountered an issue with the following code:

- $a = 10
- $b = 11
| (#{$a}
if $a != $b
  | - #{$b}
| )

I expected to get:

(10 - 11)

But I actually get:

(10
- 11
)

Which renders into (10 - 11 ).

But if we use text block, it does not result in an extra space at the end.

| (10
if true
  | - 11
| )

Results in (10 - 11).

Thanks!

char101 commented 4 years ago

I also tried using the ternary syntax but it results in wrong output as explained here https://github.com/phug-php/phug/issues/65

char101 commented 4 years ago

What works is separating the text from the variable output

- $a = 10
- $b = 11
| (
= $a
if $a != $b
  |
  | - 
  |
  = $b
| )
kylekatarnls commented 4 years ago

Regarding your first example, I don't see why ) would behave differently from - either you should expect no spaces (this is what happen in pugjs):

(10- 11)

Or spaces:

(10 - 11 )

But wanting to have the space before the - but not before the ) sounds like you want Phug to guess the meaning of your punctuation.

kylekatarnls commented 4 years ago

I recommend the following approach:

| (#{$a.($a != $b ? ' - '.$b : '')})

Or if you plan to reuse it, a mixin:

mixin numberCouple($a, $b)
  - $text = $a
  if $a != $b
    - $text .= ' - '.$b
  | (#{$text})

+numberCouple(10, 11)

+numberCouple(4, 4)
char101 commented 4 years ago

It's not about the space but about the newline added in the output when using interpolation.

Compare

| a
| b

outputs

ab

with

| #{'a'}
| #{'b'}

outputs

a
b

Which will be displayed as a b.

When using interpolation there is a newline there that is displayed as space. This make it hard if I want to just display texts without space between them.

char101 commented 4 years ago

This is the output when using pugjs

| a
| b

| #{'a'}
| #{'b'}

|a
|b

= 'a'
= 'b'
a b a b a bab

I prefer if there is no space added after | but at least it is consistent. It seems that the behaviour of pugjs is to add a space between | but it does not adds any newline.

kylekatarnls commented 4 years ago

I just prefer to warn, removing new lines would give you (10- 11) It would never give you what you expected in the first place: (10 - 11)

char101 commented 4 years ago

In my opinion, adding space is easy with just | but to remove the space as an effect of the newline is much harder.

kylekatarnls commented 4 years ago

About https://github.com/phug-php/phug/issues/64#issuecomment-610243792

Actually this is the output of your code in pugjs: Run:

pug.render(`| a
| b

| #{'a'}
| #{'b'}

|a
|b

= 'a'
= 'b'`);

Output:

a
b
a
b
a
bab

Pugjs output new lines between consecutive texts, Pug-php output the exact same result.

What you see on pugjs.org is parsed one more step with an HTML-beautifier. But if you run this in the console, you get new lines.

So I won't do anything about this since we're aligned.

But now I have to understand why it should be different when having an if in the middle and get (10- 11) with pugjs and your first example.

I guess pugjs add the new line only if text node are consecutive in the source, not in the output. It probably assume consecutive texts in source is an explicit new line the user want while statements like if are just interruption with logic in the same line of text.

kylekatarnls commented 4 years ago

Compare

| a
| b

outputs

ab

👀 How do you get that? I get 2 lines on Pug-php and Phug, all the versions I try.

kylekatarnls commented 4 years ago

if statement follows in fact the "tag" rule explained here: https://pugjs.org/language/plain-text.html#whitespace-control

kylekatarnls commented 4 years ago

It can finally be fixed with limited impact #68

kylekatarnls commented 4 years ago

1.7.1 released with this fixed.

char101 commented 4 years ago

Compare

| a
| b

outputs

ab

👀 How do you get that? I get 2 lines on Pug-php and Phug, all the versions I try.

Indeed when I tried it I got newlines. I think it is just my assumption.

char101 commented 4 years ago

With version 1.7.0

| a
if true
    | b
| c

| #{'a'}
if true
    | #{'b'}
| #{'c'}

output:

abc
a
b
c

With version 1.7.1, output:

abc
abc