Compacting/minifying dynamic html

前端 未结 6 1089
梦如初夏
梦如初夏 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:32

    django-htmlmin package is a awesome, it provides a lot of control and features moreover it supports HTML 5.

    0 讨论(0)
  • 2021-01-04 12:32

    If you're using with Nginx its Strip module is doing really great job with cleaning the html for you. It's real time and transparent. You don't need to care about at all after you set it up correctly.

    http://wiki.nginx.org/HttpStripModule

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-04 12:40

    I think a front-end development tool, such as grunt-contrib-htmlmin, should be used to minify Django template files before deploying them to production server.

    This way, the minification process happens in "build-time", and saves server CPU resource for HTML minification in "run-time".

    This tool is originally designed for minifying HTML, but also works for Django templates as I tried out.

    0 讨论(0)
  • 2021-01-04 12:41

    "I think a front-end development tool, such as grunt-contrib-htmlmin, should be used to minify Django template files before deploying them to production server."

    I had a need to do this some time ago to increase performance and decrease latency. I wrote a template minimizer that will minimize the HTML, CSS, and Javascript in your templates beforehand so that the pages are composed minimized. It integrates into Django as a Django command, understands the intricacies of the Django tempting language, and is extendable. You can find it on pypi at the link below:

    django-template-minimizer

    0 讨论(0)
  • 2021-01-04 12:52

    Looking around I just found django-pipeline, I just tried, by now, the middleware and it's just what I'm expected. It minify all html and text nodes and keep javascript nodes untouched.

    Middleware implementation and basic test result is about 4 minutes, just pip install and activate the middleware after you gzip middleware or on top of your MIDDLEWARE_CLASSES setting.

    https://github.com/cyberdelia/django-pipeline

    http://django-pipeline.readthedocs.org/en/latest/

    0 讨论(0)
提交回复
热议问题