Adding JSONP to turbogears

from tg import json_encode, response
from tg.render import _get_tg_vars

def render_jsonp(template_name, template_vars, **kwargs):
callback = template_name or kwargs.pop('callback', None) or 'callback'
for key in _get_tg_vars():
del template_vars[key]
response.headers['Content-Type'] = 'text/javascript'
return '%s(%s)' % (template_name, json_encode(template_vars))

from myapp.config.app_cfg import base_config
base_config.render_functions['jsonp'] = render_jsonp
base_config.mimetype_lookup = {'.jsonp': 'text/javascript'}

courtesy of Pederson:
https://github.com/TurboGears/tg2/issues/2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s