Login

A few jinja2 filters like django ones

Author:
brondsem
Posted:
March 13, 2009
Language:
Python
Version:
1.0
Tags:
filters template-filters jinja2
Score:
0 (after 0 ratings)

Some of this is modified from http://www.djangosnippets.org/snippets/1062/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
""" Convert linebreaks to <br/>s and escape each line.  Return value is marked 'safe' """
def linebreaksbr_escape(value):
    escaped_lines = []
    for line in value.split('\n'):
        escaped_lines.append(escape(line))
    escaped = "<br/>".join(escaped_lines)
    # mark as safe
    return Markup(escaped)

# reverse url pattern matching, and automatically urlencode parameters
# "|urlencode" filter is also available
def url(view_name, *args, **kwargs):
    escaped_args = tuple(_url_quote(arg) for arg in args)
    escaped_kwargs = dict((k, _url_quote(arg)) for k,v in kwargs)
    return url_noencode(view_name, *escaped_args, **kwargs)
def _url_quote(arg):
    if isinstance(arg,basestring):
        return urlquote(arg)
    else:
        return arg

# reverse url pattern matching
def url_noencode(view_name, *args, **kwargs):
    return reverse(view_name, args=args, kwargs=kwargs)

More like this

  1. jinja2 csrf_token extension by jasongreen 5 years, 3 months ago
  2. Sanitize HTML filter by henriklied 7 years, 12 months ago
  3. Django 1.2+ template loader for Jinja2 by SimonSapin 4 years, 10 months ago
  4. monkey-patch django to use jinja2 templates for 404/500 pages and 3rd-party apps by brondsem 6 years, 1 month ago
  5. Jinja2 integration + application specific functions/filters/tests by hasenj 6 years, 6 months ago

Comments

Please login first before commenting.