> Read the latest version of this page
Edit me on GitHub

pyramid.renderers

get_renderer(renderer_name, package=None)[source]

Return the renderer object for the renderer named as renderer_name.

You may supply a relative asset spec as renderer_name. If the package argument is supplied, a relative renderer name will be converted to an absolute asset specification by combining the package supplied as package with the relative asset specification supplied as renderer_name. If you do not supply a package (or package is None) the package name of the caller of this function will be used as the package.

render(renderer_name, value, request=None, package=None)[source]

Using the renderer specified as renderer_name (a template or a static renderer) render the value (or set of values) present in value. Return the result of the renderer’s __call__ method (usually a string or Unicode).

If the renderer name refers to a file on disk (such as when the renderer is a template), it’s usually best to supply the name as a asset specification (e.g. packagename:path/to/template.pt).

You may supply a relative asset spec as renderer_name. If the package argument is supplied, a relative renderer path will be converted to an absolute asset specification by combining the package supplied as package with the relative asset specification supplied as renderer_name. If you do not supply a package (or package is None) the package name of the caller of this function will be used as the package.

The value provided will be supplied as the input to the renderer. Usually, for template renderings, this should be a dictionary. For other renderers, this will need to be whatever sort of value the renderer expects.

The ‘system’ values supplied to the renderer will include a basic set of top-level system names, such as request, context, and renderer_name. If renderer globals have been specified, these will also be used to agument the value.

Supply a request parameter in order to provide the renderer with the most correct ‘system’ values (request and context in particular).

render_to_response(renderer_name, value, request=None, package=None)[source]

Using the renderer specified as renderer_name (a template or a static renderer) render the value (or set of values) using the result of the renderer’s __call__ method (usually a string or Unicode) as the response body.

If the renderer name refers to a file on disk (such as when the renderer is a template), it’s usually best to supply the name as a asset specification.

You may supply a relative asset spec as renderer_name. If the package argument is supplied, a relative renderer name will be converted to an absolute asset specification by combining the package supplied as package with the relative asset specification supplied as renderer_name. If you do not supply a package (or package is None) the package name of the caller of this function will be used as the package.

The value provided will be supplied as the input to the renderer. Usually, for template renderings, this should be a dictionary. For other renderers, this will need to be whatever sort of value the renderer expects.

The ‘system’ values supplied to the renderer will include a basic set of top-level system names, such as request, context, and renderer_name. If renderer globals have been specified, these will also be used to agument the value.

Supply a request parameter in order to provide the renderer with the most correct ‘system’ values (request and context in particular). Keep in mind that if the request parameter is not passed in, any changes to request.response attributes made before calling this function will be ignored.

class JSONP(param_name='callback')[source]

JSONP renderer factory helper which implements a hybrid json/jsonp renderer. JSONP is useful for making cross-domain AJAX requests.

Configure a JSONP renderer using the pyramid.config.Configurator.add_renderer() API at application startup time:

from pyramid.config import Configurator

config = Configurator()
config.add_renderer('jsonp', JSONP(param_name='callback'))

Once this renderer is registered via add_renderer() as above, you can use jsonp as the renderer= parameter to @view_config or pyramid.config.Configurator.add_view`():

from pyramid.view import view_config

@view_config(renderer='jsonp')
def myview(request):
    return {'greeting':'Hello world'}

When a view is called that uses the JSONP renderer:

  • If there is a parameter in the request’s HTTP query string that matches the param_name of the registered JSONP renderer (by default, callback), the renderer will return a JSONP response.
  • If there is no callback parameter in the request’s query string, the renderer will return a ‘plain’ JSON response.

Note

This feature is new in Pyramid 1.1.

See also: JSONP Renderer.

null_renderer

An object that can be used in advanced integration cases as input to the view configuration renderer= argument. When the null renderer is used as a view renderer argument, Pyramid avoids converting the view callable result into a Response object. This is useful if you want to reuse the view configuration and lookup machinery outside the context of its use by the Pyramid router.

Previous topic

pyramid.registry

Next topic

pyramid.request