def render(info, template=None, format=None, headers=None, mapping=None, fragment=False): """Renders data in the desired format. @param info: the data itself @type info: dict @param format: "html", "xml" or "json" @type format: string @param headers: for response headers, primarily the content type @type headers: dict @param fragment: passed through to tell the template if only a fragment of a page is desired @type fragment: bool @param template: name of the template to use @type template: string """ template = format == 'json' and 'json' or info.pop( "tg_template", template) if not info.has_key("tg_flash"): if config.get("tg.empty_flash", True): info["tg_flash"] = None engine, template, enginename = _choose_engine(template) if format: if format == 'plain': if enginename == 'genshi': format = 'text' elif format == 'text': if enginename == 'kid': format = 'plain' else: format = enginename == 'json' and 'json' or config.get( "%s.outputformat" % enginename, 'html') if isinstance(headers, dict): # Determine the proper content type and charset for the response. # We simply derive the content type from the format here # and use the charset specified in the configuration setting. # This could be improved by also examining the engine and the output. content_type = headers.get('Content-Type') if not content_type: if format: content_format = format if isinstance(content_format, (tuple, list)): content_format = content_format[0] if isinstance(content_format, str): content_format = content_format.split( )[0].split('-' , 1)[0].lower() else: content_format = 'html' else: content_format = 'html' content_type = get_mime_type_for_format(content_format) if mime_type_has_charset( content_type) and '; charset=' not in content_type: charset = get_template_encoding_default(enginename) if charset: content_type += '; charset=' + charset headers['Content-Type'] = content_type args, kw = adapt_call(engine.render, args= [], kw = dict(info=info, format=format, fragment=fragment, template=template, mapping=mapping), start=1) return engine.render(**kw)
def test_get_mime_type_for_format(): assert util.get_mime_type_for_format('plain') == 'text/plain' assert util.get_mime_type_for_format('text') == 'text/plain' assert util.get_mime_type_for_format('html') == 'text/html' assert util.get_mime_type_for_format('xhtml') == 'text/html' assert util.get_mime_type_for_format('xml') == 'text/xml' assert util.get_mime_type_for_format('json') == 'application/json' assert util.get_mime_type_for_format('foo') == 'text/html' config.update({ 'global': { 'tg.format_mime_types': { 'xhtml': 'application/xhtml+xml', 'foo': 'text/bar' } } }) assert util.get_mime_type_for_format('xhtml') == 'application/xhtml+xml' assert util.get_mime_type_for_format('json') == 'application/json' assert util.get_mime_type_for_format('foo') == 'text/bar' config.update({'global': {'tg.format_mime_types': {}}}) assert util.get_mime_type_for_format('xhtml') == 'text/html' assert util.get_mime_type_for_format('foo') == 'text/html'
def test_get_mime_type_for_format(): assert util.get_mime_type_for_format('plain') == 'text/plain' assert util.get_mime_type_for_format('text') == 'text/plain' assert util.get_mime_type_for_format('html') == 'text/html' assert util.get_mime_type_for_format('xhtml') == 'text/html' assert util.get_mime_type_for_format('xml') == 'text/xml' assert util.get_mime_type_for_format('json') == 'application/json' assert util.get_mime_type_for_format('foo') == 'text/html' config.update({'global': {'tg.format_mime_types': {'xhtml': 'application/xhtml+xml', 'foo': 'text/bar'}}}) assert util.get_mime_type_for_format('xhtml') == 'application/xhtml+xml' assert util.get_mime_type_for_format('json') == 'application/json' assert util.get_mime_type_for_format('foo') == 'text/bar' config.update({'global': {'tg.format_mime_types': {}}}) assert util.get_mime_type_for_format('xhtml') == 'text/html' assert util.get_mime_type_for_format('foo') == 'text/html'
def render(info, template=None, format=None, headers=None, fragment=False, **options): """Renders data in the desired format. @param info: the data itself @type info: dict @param template: name of the template to use @type template: string @param format: "html", "xml", "text" or "json" @type format: string @param headers: for response headers, primarily the content type @type headers: dict @param fragment: passed through to tell the template if only a fragment of a page is desired. This is a way to allow xml template engines to generate non valid html/xml because you warn them to not bother about it. @type fragment: bool All additional keyword arguments are passed as keyword args to the render method of the template engine. """ environ = getattr(cherrypy.request, 'wsgi_environ', {}) if environ.get('paste.testing', False): cherrypy.request.wsgi_environ['paste.testing_variables']['raw'] = info template = format == 'json' and 'json' or info.pop( "tg_template", template) if not info.has_key("tg_flash"): if config.get("tg.empty_flash", True): info["tg_flash"] = None engine, template, enginename = _choose_engine(template) if format: if format == 'plain': if enginename == 'genshi': format = 'text' elif format == 'text': if enginename == 'kid': format = 'plain' else: format = enginename == 'json' and 'json' or config.get( "%s.outputformat" % enginename, config.get("%s.default_format" % enginename, 'html')) if isinstance(headers, dict): # Determine the proper content type and charset for the response. # We simply derive the content type from the format here # and use the charset specified in the configuration setting. # This could be improved by also examining the engine and the output. content_type = headers.get('Content-Type') if not content_type: if format: content_format = format if isinstance(content_format, (tuple, list)): content_format = content_format[0] if isinstance(content_format, str): content_format = content_format.split( )[0].split('-' , 1)[0].lower() else: content_format = 'html' else: content_format = 'html' content_type = get_mime_type_for_format(content_format) if mime_type_has_charset( content_type) and '; charset=' not in content_type: charset = options.get('encoding', get_template_encoding_default(enginename)) if charset: content_type += '; charset=' + charset headers['Content-Type'] = content_type args, kw = adapt_call(engine.render, args=[], kw=dict( info=info, format=format, fragment=fragment, template=template, **options), start=1) return engine.render(**kw)