Generate Javascript-Safe string in template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import os
from django.template.defaultfilters import escape

@register.filter(name="tojavascript")
def tojavascript (value) :
	if len(value.split(os.linesep)) > 1 :
		__s = ["\"%s\"" % escape(i) for i in value.split(os.linesep)]
		return "%s\n%s" % (__s[0], "\n".join([" + %s" % i for i in __s[1:]]), )
	else :
		return "\"%s\"" % escape(value)

More like this

  1. Truncate filter by zalun 3 years ago
  2. Word-boundary-aware string truncation template filter by josho 2 years, 12 months ago
  3. Email obfuscation filter using ROT13 by worksology 3 years ago
  4. Filter change list by a date range by aruseni 3 months, 1 week ago
  5. truncatestring filter by pigletto 3 years, 2 months ago

Comments

guychi (on August 26, 2007):

doesn't seem to work.

the code is also exceedingly unclear, perhaps you'd care to explain it a bit?

#

(Forgotten your password?)