sqlalchemy / mako

Mako Templates for Python
https://www.makotemplates.org
MIT License
353 stars 60 forks source link

remove whitespace #390

Closed fkv1 closed 7 months ago

fkv1 commented 7 months ago

The Template Toolkit provides an easy syntax for removing leading and trailing whitespace: [%- instead of [% removes all whitespace (including newlines) before the directive -%] instead of %] removes all whitespace (including newlines) after the directive

The latter is barely ever needed, but the removal of leading whitespace is very important.

This feature is missing in Mako. You can only add a backslash at the end of the line to eat the newline. But that's insufficient in several respects:

  1. It's counter-intuitive. If you insert a block, you don't want to edit other lines to compensate for the effects of the inserted block. Same when you remove the block. There's a high probability you'll forget to remove the backslash in the preceding line.
  2. diffs and patches also become more confusing if the preceding line needs to be changed when you remove or insert a block.
  3. if you have:
    <div>
    aaa\
    <%
        (some python code)
    %>
    bbb
    </div>

    or

    <div>
    aaa
    <%
        (some python code)
    %>\
    bbb
    </div>

    you get:

    <div>
    aaa
        bbb
    </div>

    (because only the newline is chomped, not the spaces)

but you want:

<div>
    aaa
    bbb
</div>

Of course you can remove the indentation of the Mako block, but at the cost of readability.

The Template Toolkit approach would be:

<div>
    aaa
    <%-
        (some python code)
    %>
    bbb
</div>

Can this be implemented?