Login

A few jinja2 filters like django ones

Author:
brondsem
Posted:
March 13, 2009
Language:
Python
Version:
1.0
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. My firs Snippets by GutemaG 3 days, 3 hours ago
  2. FileField having auto upload_to path by junaidmgithub 1 month, 1 week ago
  3. LazyPrimaryKeyRelatedField by LLyaudet 1 month, 2 weeks ago
  4. CacheInDictManager by LLyaudet 1 month, 2 weeks ago
  5. MYSQL Full Text Expression by Bidaya0 1 month, 2 weeks ago

Comments

Please login first before commenting.