django-linestripper removes blank / empty / whitespaced lines generated by template tags.
django == 1.4
<h1>My list</h1>
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
<h1>My list</h1>
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
<h1>My list</h1>
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
- Put the
django_linestripper
folder at the root of your app structure - Edit
settings.py
: - In
TEMPLATE_LOADERS
, adddjango_linestripper.stripper.Loader
as the first loader - In
MIDDLEWARE_CLASSES
, putdjango_linestripper.stripper.StripperMiddleware
wherever you want - You're done !
The following constants can be overriden in settings.py
:
defaults to True
Indicates weither django-linestripper is enabled or not.
defaults to "__STRIPPER_TAG__"
A unique string that you don't use in your templates. It will be suppressed by the django-linestripper middleware.
defaults to False
Clear every non-generated whitespace metacharacter before \n
. For instance:
\n\t\t\t\n
=> \n\t\t\t\n
\n\t\t\t\n
=> \n\n
- Step 1: tag empty lines with
__STRIPPER_TAG__
- Step 2: wait for the rendered template
- Step 3: suppress blank lines and suppress
__STRIPPER_TAG__
If your rendered template isn't part of the HttpResponse, the stripper middleware won't intercept the rendering. It can be a pain if you're using templates as mail templates.
This thread on DjangoProject.com talks about the subject in a very precise manner. A patch has been submitted but the validation process is very long. Please do not hesitate in contacting me when this whitespace issue is resolved.
I always dreamed of doing that.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Loïs Di Qual <lois[at]di-qual.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.