-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Whitespace control - Trim left spaces #3683
Comments
I have question about whitespace control related to this. My test scenario:
And output is
It not documented and lead to some kinds of errors. For example it can be dangerous in js:
and instead
we got a wrong js-code
|
I make demo on raw php and twig And twig already uses different approaches for removing line endings after output and execution tags. In php, removing line endings is useful for a few reasons. This was discussed on the mailing list in 1998 (25 years ago!) https://marc.info/?t=90279165800002&r=1&w=2 But why does this rule about deleting a newline after closing a tag exist in Twig? |
This is actually documented. It is the first sentence in the documentation section that I linked to you:
|
Yes, sorry about that. Misunderstood the meaning and thought about the line ending at end of template. But the question is still open: Why is this principle from php is used in twig? And how is it possible to disable it globally? |
you cannot disable it globally. |
If an expression
{{ }}
or statement{% %}
is directly followed by a linebreak, which is pretty often, it seems to be automatically removed.For instance:
would output:
and not:
If we extend this reasoning, wouldn't it make sense to also trim spaces at the left of the expression/statement, in the case there are only spaces at the start of the line?
To illustrate, let's reuse previous example with indentation:
It now outputs:
Currently, to maintain a good indentation, I always use a left space-only trim
~
:The use of
~
being pretty much systematic, I think it would be fully legitimate to infer it when the left part of the current line contains only spaces. I would agree that deciding to remove left spaces would mean that we would force the removal of "information" that, in some case that I cannot think of at the moment, some might actually want to preserve. Then, once again, Twig already does something similar by implicitly removing some linebreaks.Therefore, I think a more natural way to deduce removable content would be to arbitrate on the whole line: if only composed of spaces apart from the statement, the whole line would be trimmed, including the ending linebreak, otherwise none of it. Moreover, this rule makes sense on a statement or comment, but not so much on an expression, which is expected to output something.
Two last exemples:
outputs:
whilst I believe one could legitimately expect to obtain instead:
And:
outputs:
whilst we could expect instead:
To sum up, the idea is quite simple: if a whole line (including the finishing CR/LF) is only composed of spaces, comments and statements, it shouldn't be rendered. Otherwise, everything should be preserved (including the finishing CR/LF).
What do you think?
The text was updated successfully, but these errors were encountered: