Frequently used tags/filters for Jinja2

 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
from django.template.defaulttags import URLNode
from django.conf import settings
from jinja2.filters import contextfilter
from django.utils import translation



def url(view_name, *args, **kwargs):
    from django.core.urlresolvers import reverse, NoReverseMatch
    try:
        return reverse(view_name, args=args, kwargs=kwargs)
    except NoReverseMatch:
        try:
            project_name = settings.SETTINGS_MODULE.split('.')[0]
            return reverse(project_name + '.' + view_name,
                           args=args, kwargs=kwargs)
        except NoReverseMatch:
            return ''

def nbspize(text):
    import re
    return re.sub('\s',' ',text.strip())

def get_lang():
    return translation.get_language()

def timesince(date):
    from django.utils.timesince import timesince
    return timesince(date)

def timeuntil(date):
    from django.utils.timesince import timesince
    from datetime import datetime
    return timesince(datetime.now(),datetime(date.year, date.month, date.day))

def truncate(text,arg):
    import re
    from django.utils.encoding import force_unicode
    text = force_unicode(text)
    matches = re.match('(\d+)([cw]{1})',arg)
    if not matches:
        return text
    count = int(matches.group(1))
    type = matches.group(2)

    if type == 'c':
        if count > len(text):
            return text
        else:
            return text[:count] + '…'
    elif type == 'w':
        arr = text.strip().split()
        if count > len(arr):
            return ' '.join(arr)
        else:
            return ' '.join(arr[:count]) + '…'

More like this

  1. Django 1.2+ template loader for Jinja2 by SimonSapin 3 years, 10 months ago
  2. A few jinja2 filters like django ones by brondsem 5 years, 1 month ago
  3. Jinja2 integration + application specific functions/filters/tests by hasenj 5 years, 6 months ago
  4. truncate letters by trbs 7 years, 1 month ago
  5. Truncate filter by zalun 4 years, 12 months ago

Comments

brondsem (on January 27, 2009):

Thanks. And you can also reuse some directly from django. For example you can register these methods in env.filters: django.template.defaultfilters.linebreaks, django.template.defaultfilters.linebreaksbr, django.template.defaultfilters.urlencode

And if you want url() to automatically encode all parameters (not sure if the django tags does this, but I wanted it for my jinja filter), rename "def url" to "def url_noencode" and add:

# automatically urlencode parameters
# "|urlencode" filter is also available, but it doesn't do / (see urllib.quote)
def url(view_name, *args, **kwargs):
    import urllib
    escaped_args = tuple(urllib.quote(arg,"") if isinstance(arg,basestring) else arg for arg in args)
    escaped_kwargs = dict((k, urllib.quote(v,"") if isinstance(arg,basestring) else arg) for k,v in kwargs)
    return url_noencode(view_name, *escaped_args, **kwargs)

#

(Forgotten your password?)