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

10.7. OVERRIDING A RENDERER AT RUNTIME

1 config.add_renderer(’.pt’, ’mypackage.pt_renderer’)

After you do this, the renderer factory in mypackage.pt_renderer will be used to render templates which end in .pt, replacing the default Chameleon ZPT renderer.

To associate a default renderer with all view configurations (even ones which do not possess a renderer attribute), pass None as the name attribute to the renderer tag:

1 config.add_renderer(None, ’mypackage.json_renderer_factory’)

10.7 Overriding A Renderer At Runtime

latex-warning.png

This is an advanced feature, not typically used by “civilians”.

In some circumstances, it is necessary to instruct the system to ignore the static renderer declaration provided by the developer in view configuration, replacing the renderer with another after a request starts. For example, an “omnipresent” XML-RPC implementation that detects that the request is from an XML-RPC client might override a view configuration statement made by the user instructing the view to use a template renderer with one that uses an XML-RPC renderer. This renderer would produce an XML-RPC representation of the data returned by an arbitrary view callable.

To use this feature, create a NewRequest subscriber which sniffs at the request data and which conditionally sets an override_renderer attribute on the request itself, which is the name of a registered renderer. For example:

1

2

3

4

5

6

7

from pyramid.event import subscriber from pyramid.event import NewRequest

@subscriber(NewRequest)

def set_xmlrpc_params(event): request = event.request

if (request.content_type == ’text/xml’

119

10. RENDERERS

8and request.method == ’POST’

9and not ’soapaction’ in request.headers

10and not ’x-pyramid-avoid-xmlrpc’ in request.headers):

11params, method = parse_xmlrpc_request(request)

12request.xmlrpc_params, request.xmlrpc_method = params, method

13request.is_xmlrpc = True

14request.override_renderer = ’xmlrpc’

15return True

The result of such a subscriber will be to replace any existing static renderer configured by the developer with a (notional, nonexistent) XML-RPC renderer if the request appears to come from an XML-RPC client.

120

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