Browse files

added urlencode filter, this commit fixes #17

  • Loading branch information...
1 parent 2caef64 commit 37303a86583eda14fb61b14b4922bdce073bce57 Grygoriy Fuchedzhy committed Oct 1, 2011
Showing with 8 additions and 0 deletions.
  1. +8 −0 jinja2/
@@ -10,6 +10,7 @@
import re
import math
+from urllib import quote, quote_plus
from random import choice
from operator import itemgetter
from itertools import imap, groupby
@@ -394,6 +395,12 @@ def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False):
rv = Markup(rv)
return rv
+def do_urlencode(value, plus=False):
+ """Return the urlencoded value. For detailed informations have a look at the
+ help page of "urllib.quote". If plus is set to True it will use the
+ "urllib.quote_plus" method.
+ """
+ return quote_plus(value) if plus else quote(value)
snoack Nov 2, 2011 Contributor

The ... if ... else ... syntax isn't compatible with Python 2.4.

def do_indent(s, width=4, indentfirst=False):
"""Return a copy of the passed string, each line indented by
@@ -787,6 +794,7 @@ def do_attr(environment, obj, name):
'string': soft_unicode,
'list': do_list,
'urlize': do_urlize,
+ 'urlencode': do_urlencode,
snoack Oct 28, 2011 Contributor

Why do you call the filter urlencode if its actually urllib.quote, not urllib.urlencode?

gfuchedzhy Oct 28, 2011

because it was called this way in jinja-1

'format': do_format,
'trim': do_trim,
'striptags': do_striptags,

2 comments on commit 37303a8


This is not unicode safe. If we would integrate such a filter we would have to select a default encoding. Not sure what to do there. Maybe force UTF-8 on everybody.

snoack commented on 37303a8 Nov 2, 2011

I also don't like the plus argument, because of that makes the choice whether to use the plus-variant or not up to the template author. But your application might require that all urls are quoted the same certain way.

In my opinion it would be appropriate to leave the implementation of that filter to the user, instead of providing a default implementation that either makes decisions (about the encoding and whether or not to use the plus-variant) for the user or requires them as argument, each time the filter is used.

Please sign in to comment.