Login

Smart Spaceless

Author:
btaylordesign
Posted:
April 8, 2011
Language:
Python
Version:
1.2
Score:
4 (after 4 ratings)

This 'smart_spaceless' template tag is a replacement for Django's built-in 'spaceless'. If settings.DEBUG = True, spaces will not be removed to make debugging your template code easier. When DEBUG = False, spaces will be removed.

Happy coding!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
This example assumes you have placed this code into a file called "template_additions.py"
and placed that folder into a "templatetags" folder inside a module.
It can live wherever you need.

Example folder structure:
/lib/
    /__init__.py
    /templatetags/
        /__init__.py
        /template_additions.py
"""

from django.conf import settings
from django import template
from django.utils.html import strip_spaces_between_tags

register = template.Library()

class SmartSpacelessNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        content = self.nodelist.render(context)
        return content if settings.DEBUG else strip_spaces_between_tags(content.strip())

@register.tag
def smart_spaceless(parser, token):
    """
    Removes whitespace between HTML tags, including tab and newline characters,
    but only if settings.DEBUG = False

    Example usage:
        {% load template_additions %}
        {% smart_spaceless %}
            <p>
                <a href="foo/">Foo</a>
            </p>
        {% end_smart_spaceless %}

    This example would return this HTML:

        <p><a href="foo/">Foo</a></p>

    Only space between *tags* is normalized -- not space between tags and text.
    In this example, the space around ``Hello`` won't be stripped:

        {% smart_spaceless %}
            <strong>
                Hello
            </strong>
        {% end_smart_spaceless %}
    """
    nodelist = parser.parse(('end_smart_spaceless',))
    parser.delete_first_token()
    return SmartSpacelessNode(nodelist)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

matiascelasco (on November 11, 2014):

How did you manage to use a templatetag directory that is not inside a django app? Django documentation says that "custom template tags and filters must live inside a Django app" (https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/)

#

btaylordesign (on February 4, 2015):

@matiascelasco

You would still need to include the template tag in an app directory. For code like this, I usually have a "lib" app that I drop it into.

#

Please login first before commenting.