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 thepackage
argument is supplied, a relative renderer name will be converted to an absolute asset specification by combining the package supplied aspackage
with the relative asset specification supplied asrenderer_name
. If you do not supply apackage
(orpackage
isNone
) 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 invalue
. 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 thepackage
argument is supplied, a relative renderer path will be converted to an absolute asset specification by combining the package supplied aspackage
with the relative asset specification supplied asrenderer_name
. If you do not supply apackage
(orpackage
isNone
) 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
,renderer_name
, andview
. See System Values Used During Rendering for the full list. 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
andcontext
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 thepackage
argument is supplied, a relative renderer name will be converted to an absolute asset specification by combining the package supplied aspackage
with the relative asset specification supplied asrenderer_name
. If you do not supply apackage
(orpackage
isNone
) 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
,renderer_name
, andview
. See System Values Used During Rendering for the full list. 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
andcontext
in particular). Keep in mind that if therequest
parameter is not passed in, any changes torequest.response
attributes made before calling this function will be ignored.
-
class
JSON
(serializer=<function dumps>, adapters=(), **kw)[source]¶ Renderer that returns a JSON-encoded string.
Configure a custom JSON renderer using the
add_renderer()
API at application startup time:from pyramid.config import Configurator config = Configurator() config.add_renderer('myjson', JSON(indent=4))
Once this renderer is registered as above, you can use
myjson
as therenderer=
parameter to@view_config
oradd_view`()
:from pyramid.view import view_config @view_config(renderer='myjson') def myview(request): return {'greeting':'Hello world'}
Custom objects can be serialized using the renderer by either implementing the
__json__
magic method, or by registering adapters with the renderer. See カスタムオブジェクトのシリアライズ for more information.The default serializer uses
json.JSONEncoder
. A different serializer can be specified via theserializer
argument. Custom serializers should accept the object, a callbackdefault
, and any extrakw
keyword argments passed during renderer construction.Note
This feature is new in Pyramid 1.4. Prior to 1.4 there was no public API for supplying options to the underlying serializer without defining a custom renderer.
-
class
JSONP
(param_name='callback', **kw)[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'))
The class’ constructor also accepts arbitrary keyword arguments. All keyword arguments except
param_name
are passed to thejson.dumps
function as its keyword arguments.from pyramid.config import Configurator config = Configurator() config.add_renderer('jsonp', JSONP(param_name='callback', indent=4))
Note
The ability of this class to accept a
**kw
in its constructor is new as of Pyramid 1.4.The arguments passed to this class’ constructor mean the same thing as the arguments passed to
pyramid.renderers.JSON
(includingserializer
andadapters
).Once this renderer is registered via
add_renderer()
as above, you can usejsonp
as therenderer=
parameter to@view_config
orpyramid.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 レンダラー.
- If there is a parameter in the request’s HTTP query string that matches
the
-
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.