Hi all,
Currently working on an app where I want to render JSONP, so that I
can pull app data from various sites without having to do without JSON
goodness.
Here's the very simple renderer I came up with (file is called myapp/
tgext.py):
# -*- coding: utf-8 -*-
"""Extensions to TurboGears functionality"""
import simplejson
from turbojson import jsonify
exclude_keys = ('c', 'helpers', 'url', 'tg', 'request',
'tmpl_context',
'response')
def render_jsonp(template_name, template_vars, **kwargs):
result_dict = {}
result_dict.update(template_vars)
for exclude_key in exclude_keys:
result_dict.pop(exclude_key)
json = jsonify.encode(result_dict)
return "%s(%s)" % (template_vars['tg']['inputs']['jsoncallback'],
json)
... which is then added in the config (myapp/config/app_cfg.py) with
these lines:
from myapp.tgext import render_jsonp
base_config.render_functions['jsonp'] = render_jsonp
and lastly, the controller methods are exposed using:
@expose('jsonp:')
NOTE: the current implementation presupposes that you pass in the
JSONP callback method in the URL parameter 'jsoncallback'. E.g.
http://my.site.com/path/to/controller?jsoncallback=mycallback
... just thought I'd post it incase it could be useful to someone
else :)))
Also, I'm not entirely happy with the way I'm excluding the TG dict
keys in the render function - so if anyone wants to chip in a better
solution, I'd be extremely grateful ;)
And lastly - if there's already an existing solution which I've simply
missed, then please do point me in its direction! :)
Cheers,
Clive
--
You received this message because you are subscribed to the Google Groups
"TurboGears" 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/turbogears?hl=en.