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. Q marshaller by Spike^ekipS 5 years, 2 months ago
  2. Simple views method binding by Spike^ekipS 4 years, 11 months ago
  3. Dynamical formsets by I159 1 year, 6 months ago
  4. Safe template decorator by eternicode 1 year ago
  5. Filter for adding quote marks by olau 5 years, 5 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?)