Twig icon indicating copy to clipboard operation
Twig copied to clipboard

The counter intuitive removal of first newline immediately after the regular comment closing #}

Open RomanPekhov opened this issue 4 weeks ago • 4 comments

The doc/templates.rst states: "... default removal of the first newline inherited from PHP".

What are you talking about? Where PHP removes the first newline?   I cannot google up anything like such a newline removal of PHP.

I think, this "default removal of the first newline" should be removed both from the Lexer class and from the doc.

RomanPekhov avatar Dec 02 '25 19:12 RomanPekhov

Yeah I never understood that sentence either. PHP as a language doesn't enforce anything about the first newline - it would not make any sense - and PHP as a runtime doesn't remove any line either - that would make even less sense.

ericmorand avatar Dec 03 '25 06:12 ericmorand

https://3v4l.org/1YtvO

jdreesen avatar Dec 03 '25 08:12 jdreesen

I get your point and I agree: PHP removes the first new line after a PHP tag, which is what Twig also does.

Note however that it does not inherit this behavior from PHP since Twig is not a subset of PHP. This is a deliberate choice from Twig maintainers. Actually I think this is inherited from Jinja, not PHP, since Twig is a Jinja subset, which syntax is inspired by Python.

Also note that Jinja documentation actually uses a more correct wording that Twig documentation - like instead of inherited from:

If an application configures Jinja to trim_blocks, the first newline after a template tag is removed automatically (like in PHP).

https://jinja.palletsprojects.com/en/stable/templates/#

ericmorand avatar Dec 03 '25 10:12 ericmorand

@ericmorand feel free to send a PR to improve the wording of the documentation.

stof avatar Dec 03 '25 10:12 stof