Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9701 -- Added a "safeseq" template filter.

This is like "safe", except it operates on the individual elements of a
sequence, rather than treating the whole argument as a string.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9952 bcc190cf-cafb-0310-a4f2-bffc1f526a37
commit dfddf129f07a859f52f04497f6254b675a345f8d 1 parent 851461a
Malcolm Tredinnick authored March 02, 2009
11  django/template/defaultfilters.py
@@ -422,11 +422,19 @@ def safe(value):
422 422
     """
423 423
     Marks the value as a string that should not be auto-escaped.
424 424
     """
425  
-    from django.utils.safestring import mark_safe
426 425
     return mark_safe(value)
427 426
 safe.is_safe = True
428 427
 safe = stringfilter(safe)
429 428
 
  429
+def safeseq(value):
  430
+    """
  431
+    A "safe" filter for sequences. Marks each element in the sequence,
  432
+    individually, as safe, after converting them to unicode. Returns a list
  433
+    with the results.
  434
+    """
  435
+    return [mark_safe(force_unicode(obj)) for obj in value]
  436
+safeseq.is_safe = True
  437
+
430 438
 def removetags(value, tags):
431 439
     """Removes a space separated list of [X]HTML tags from the output."""
432 440
     tags = [re.escape(tag) for tag in tags.split()]
@@ -876,6 +884,7 @@ def pprint(value):
876 884
 register.filter(random)
877 885
 register.filter(rjust)
878 886
 register.filter(safe)
  887
+register.filter(safeseq)
879 888
 register.filter('slice', slice_)
880 889
 register.filter(slugify)
881 890
 register.filter(stringformat)
15  docs/ref/templates/builtins.txt
@@ -1330,6 +1330,21 @@ safe
1330 1330
 Marks a string as not requiring further HTML escaping prior to output. When
1331 1331
 autoescaping is off, this filter has no effect.
1332 1332
 
  1333
+.. templatefilter:: safeseq
  1334
+
  1335
+safeseq
  1336
+~~~~~~~
  1337
+
  1338
+Applies the :tfilter:`safe` filter to each element of a sequence.  Useful in
  1339
+conjunction with other filters that operate on sequences, such as
  1340
+:tfilter:`join`.  For example::
  1341
+
  1342
+    {{ some_list|safeseq|join:", " }}
  1343
+
  1344
+You couldn't use the :tfilter:`safe` filter directly in this case, as it would
  1345
+first convert the variable into a string, rather than working with the
  1346
+individual elements of the sequence.
  1347
+
1333 1348
 .. templatefilter:: slice
1334 1349
 
1335 1350
 slice
3  tests/regressiontests/templates/filters.py
@@ -199,6 +199,9 @@ def get_filter_tests():
199 199
         'filter-safe01': ("{{ a }} -- {{ a|safe }}", {"a": u"<b>hello</b>"}, "&lt;b&gt;hello&lt;/b&gt; -- <b>hello</b>"),
200 200
         'filter-safe02': ("{% autoescape off %}{{ a }} -- {{ a|safe }}{% endautoescape %}", {"a": "<b>hello</b>"}, u"<b>hello</b> -- <b>hello</b>"),
201 201
 
  202
+        'filter-safeseq01': ('{{ a|join:", " }} -- {{ a|safeseq|join:", " }}', {"a": ["&", "<"]}, "&amp;, &lt; -- &, <"),
  203
+        'filter-safeseq02': ('{% autoescape off %}{{ a|join:", " }} -- {{ a|safeseq|join:", " }}{% endautoescape %}', {"a": ["&", "<"]}, "&, < -- &, <"),
  204
+
202 205
         'filter-removetags01': ('{{ a|removetags:"a b" }} {{ b|removetags:"a b" }}', {"a": "<a>x</a> <p><b>y</b></p>", "b": mark_safe("<a>x</a> <p><b>y</b></p>")}, u"x &lt;p&gt;y&lt;/p&gt; x <p>y</p>"),
203 206
         'filter-removetags02': ('{% autoescape off %}{{ a|removetags:"a b" }} {{ b|removetags:"a b" }}{% endautoescape %}', {"a": "<a>x</a> <p><b>y</b></p>", "b": mark_safe("<a>x</a> <p><b>y</b></p>")}, u"x <p>y</p> x <p>y</p>"),
204 207
 

0 notes on commit dfddf12

Please sign in to comment.
Something went wrong with that request. Please try again.