Задать вопрос

Тел: +7 965 3737 888





Template tag for stripping blank lines

When writing clean and easy-to-read templates, it's usually good to have structural template tags (e.g. {%for%}, {%if%}) alone on their own line with proper indentation.

When such a template is rendered, the resulting HTML will have blank lines in place of the template tags. The leading blank space used for indentation is also intact.

This template tag strips all empty and all-whitespace lines when rendering the template. Be careful not to apply it when not intended, e.g. when empty lines are wanted inside PRE tags.

Вопрос полезен? Да0/Нет0

Ответы (1):

Ответ полезен? Да0/Нет0

mikko, strange that I didn't find your snippet when I tried to search for one before writing mine.

Actually I think this solution, where blank lines are removed after rendering the lines, is not optimal, because also blank lines inserted on purpose are now lost.

It would be neat if lines with nothing but a template tag (and blank space) were detected and stripped of whitespace and the line feed. I don't have an idea how to implement that though. Probably also tags with non-empty output should retain their indentation.