Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
22
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

CHAPTER

FIFTYTWO

PYRAMID.REGISTRY

class Registry(name=’‘, bases=())

A registry object is an application registry. It is used by the framework itself to perform mappings of URLs to view callables, as well as servicing other various framework duties. A registry has its own internal API, but this API is rarely used by Pyramid application developers (it’s usually only used by developers of the Pyramid framework). But it has a number of attributes that may be useful to application developers within application code, such as settings, which is a dictionary containing application deployment settings.

For information about the purpose and usage of the application registry, see Using the Zope Component Architecture in Pyramid.

The application registry is usually accessed as request.registry in application code.

settings

The dictionary-like deployment settings object. See Deployment Settings for information. This object is often accessed as request.registry.settings or config.registry.settings in a typical Pyramid application.

introspector

 

When a

registry is set up (or created) by a Configurator,

the registry

will

be

decorated with an instance named introspector

implement-

ing

the

pyramid.interfaces.IIntrospector interface.

See also

pyramid.config.Configurator.introspector‘.

When a registry is created “by hand”, however, this attribute will not exist until set up by a configurator.

This attribute is often accessed as request.registry.introspector in a typical Pyramid application.

This attribute is new as of Pyramid 1.3.

611

52. PYRAMID.REGISTRY

class Introspectable

The default implementation of the interface pyramid.interfaces.IIntrospectable used by framework exenders. An instance of this class is is created when pyramid.config.Configurator.introspectable is called.

This class is new as of Pyramid 1.3.

612

CHAPTER

FIFTYTHREE

PYRAMID.RENDERERS

get_renderer(renderer_name, package=None)

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)

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).

613

53. PYRAMID.RENDERERS

render_to_response(renderer_name, value, request=None, package=None)

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’)

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‘():

614

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.

latex-note.png

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.

615

53. PYRAMID.RENDERERS

616

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]