Closed char101 closed 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
What works is separating the text from the variable output
- $a = 10
- $b = 11
| (
= $a
if $a != $b
|
| -
|
= $b
| )
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.
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)
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.
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.
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)
In my opinion, adding space is easy with just |
but to remove the space as an effect of the newline is much harder.
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.
Compare
| a | b
outputs
ab
👀 How do you get that? I get 2 lines on Pug-php and Phug, all the versions I try.
if
statement follows in fact the "tag" rule explained here:
https://pugjs.org/language/plain-text.html#whitespace-control
It can finally be fixed with limited impact #68
1.7.1 released with this fixed.
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.
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
Hello,
I encountered an issue with the following code:
I expected to get:
But I actually get:
Which renders into
(10 - 11 )
.But if we use text block, it does not result in an extra space at the end.
Results in
(10 - 11)
.Thanks!