Compacting/minifying dynamic html

前端 未结 6 1169
梦如初夏
梦如初夏 2021-01-04 12:21

I am using django templating, and for readability I have html that looks similar to the following:

{% if some_variable %}
    text
{% else %}
    nothing exi         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 12:33

    For a bit of background, this is done intentionally, since Django's templating engine currently doesn't care about sanitizing lines that evaluate to blanks after the tags get stripped out and doing so will incur some minor performance penalties in serving the response, since it involves a post-processing step that evaluates the full contents of the rendered template.

    Still, if you need a quick solution, I suggest you employ the StripWhitespaceMiddleware response middleware that'll remove spurious whitespaces from textual responses. It's pretty fast and straightforward, employing regular expression matching like the templating engine itself. A more taxing, but powerful alternative would be deploy a response middleware that utilizes an HTML prettifier, if you really care about the poor human beings still reading raw responses.

提交回复
热议问题