0

I am trying to pass a python dictionary from a chameleon template to a javascript function. But since the dictionary contains single quotes or ' which need to be escaped I get an error in firebug that says : SyntaxError: missing ) after argument list. My code looks like this:

<div id = "divsfp"> <input type="button" id="sfp" value="SFP" onclick="get_sfp('${dict_value}')"></input></div>

Where dict_value is a python dictionary. How can I escpae ' in chameleon template before passing the data or in Javascript function itself?

cisnik
  • 107
  • 10

3 Answers3

1

You need to JSON encode the dictionary. You don't then need to put quotes around the dictionary, and JavaScript will see it as a JavaScript object.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

Use double-quotes, encoded as &quot:

onclick="get_sfp(&quot;${dict_value}&quot;)"

Chameleon will escape any double-quotes in dict_value.

malthe
  • 1,237
  • 13
  • 25
0

You can try this, if this helps

"get_sfp('"+${dict_value}+"')"

Also from your implementation it seems that the dict_value is the variable you already know. So whats the problem accessing it from the get_sfp function.

Sorry couldn't comment as I still don't have that privilege.

Pallab
  • 285
  • 1
  • 7
  • This is not a python expression, the `"` quotes are delimiting an *XML attribute*. – Martijn Pieters Jul 26 '13 at 08:14
  • @Pallab That doesn't work. It throws an error in template saying `ParseError: Unexpected end tag` (think because of the reason mentioned by Martijn). – cisnik Jul 26 '13 at 09:22