Login

whitespaceoptimize block tag

Author:
peterbe
Posted:
September 14, 2008
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

This is a custom block tag and is used like this:

{% load whitespaceoptimize %}
{% whitespaceoptimize "css" %}
/* CSS comment */
body {
   color: #CCCCCC; 
}
{% endwhitespaceoptimize %}

And when rendered you get this output:

body{color:#CCC}

To install it, download the snippet and call it myapp/templatetags/whitespaceoptimize.py. (Make sure you have a __init__.py in the templatetags directory) You need to download and install slimmer and put on your Python path.

The block tag can be used for javascript and html as well as css. You can also let it guess the format; this would work for example:

{% whitespaceoptimize %}
<table> 
   <tr> ...
{% endwhitespaceoptimize %}

...but this would just replicate the functonality of the built-in spaceless tag

 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
# python
from slimmer import guessSyntax, css_slimmer, html_slimmer, js_slimmer

# django
from django import template
    
class WhitespaceOptimizeNode(template.Node):
    def __init__(self, nodelist, format=None):
        self.nodelist = nodelist
        self.format = format
    def render(self, context):
        code = self.nodelist.render(context)
        if self.format == 'css':
            return css_slimmer(code)
        elif self.format in ('js', 'javascript'):
            return js_slimmer(code)
        elif self.format == 'html':
            return html_slimmer(code)
        else:
            format = guessSyntax(code)
            if format:
                self.format = format
                return self.render(context)
    
        return code
    
register = template.Library()
@register.tag(name='whitespaceoptimize')
def do_whitespaceoptimize(parser, token):
    nodelist = parser.parse(('endwhitespaceoptimize',))
    parser.delete_first_token()

    _split = token.split_contents()
    format = ''
    if len(_split) > 1:
        tag_name, format = _split
        if not (format[0] == format[-1] and format[0] in ('"', "'")):
            raise template.TemplateSyntaxError, \
                    "%r tag's argument should be in quotes" % tag_name

    return WhitespaceOptimizeNode(nodelist, format[1:-1])

More like this

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

Comments

peterbe (on September 14, 2008):

The slimmer is very fast but will nevertheless cost a few milliseconds on each slimming. Adding a memoize pattern shouldn't be too hard. However, you have to keep in mind that the reason for doing this is not to obscure css, js or html but to make the downloadable object smaller which would save bandwidth.

#

Please login first before commenting.