On Sunday, March 20, 2011 11:46:10 AM UTC-7, gazza wrote:
>
> Hiya, 
>
> I want to pass a c object to a javascript onLoad function. How can I 
> do this, if at all?
>

What I do is something like this:

In controller action:

c.thing = thing

In Mako template:

<script>
  var thing = JSON.parse(${h.to_json_string(c.thing)});
</script> 

In helpers module:

def to_json_string(obj):
    return literal(repr(json.dumps(obj)))

I use this helper because it properly backslash escapes things and also adds 
the quotes around the JSON string, so what gets rendered is something like this:

<script>
  var thing = JSON.parse('{"id": 13, "last_name": "O\'Leary"}');
</script>

Another option would be to render a JS file using Mako (being sure to set 
the correct content type).

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to