Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Removes blank / empty / whitespaced lines generated by Django's template tags
branch: master

README.md

django-linestripper

django-linestripper removes blank / empty / whitespaced lines generated by template tags.

Compatibility:

django == 1.4

Example

Template

 <h1>My list</h1>

 <ul>
 {% for item in items %}
     <li>{{ item }}</li>
 {% endfor %}
 </ul>

Rendered without django-linestripper

<h1>My list</h1>

<ul>

    <li>item 1</li>

    <li>item 2</li>

    <li>item 3</li>

</ul>

Rendered with django-linestripper

<h1>My list</h1>

<ul>
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
</ul>

Setup instructions

  • Put the django_linestripper folder at the root of your app structure
  • Edit settings.py:
    • In TEMPLATE_LOADERS, add django_linestripper.stripper.Loader as the first loader
    • In MIDDLEWARE_CLASSES, put django_linestripper.stripper.StripperMiddleware wherever you want
  • You're done !

Advanced usage

The following constants can be overriden in settings.py:

STRIPPER_ENABLED

defaults to True

Indicates weither django-linestripper is enabled or not.

STRIPPER_TAG

defaults to "__STRIPPER_TAG__"

A unique string that you don't use in your templates. It will be suppressed by the django-linestripper middleware.

STRIPPER_CLEAR_LINE

defaults to False

Clear every non-generated whitespace metacharacter before \n. For instance:

With STRIPPER_CLEAR_LINE = False
\n\t\t\t\n
=> \n\t\t\t\n
With STRIPPER_CLEAR_LINE = True
\n\t\t\t\n
=> \n\n

How it works

  • Step 1: tag empty lines with __STRIPPER_TAG__
  • Step 2: wait for the rendered template
  • Step 3: suppress blank lines and suppress __STRIPPER_TAG__

Known bugs

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.

Notes

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.

License

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.
Something went wrong with that request. Please try again.