|
1 |
|
2 ===================== |
|
3 pyams_pagelet package |
|
4 ===================== |
|
5 |
|
6 Let's start by creating a new template: |
|
7 |
|
8 >>> from pyramid.testing import setUp, tearDown |
|
9 >>> from pyams_utils.request import get_annotations |
|
10 >>> config = setUp() |
|
11 >>> config.add_request_method(get_annotations, 'annotations', reify=True) |
|
12 |
|
13 >>> import os, tempfile |
|
14 >>> temp_dir = tempfile.mkdtemp() |
|
15 |
|
16 >>> content_template = os.path.join(temp_dir, 'content-template.pt') |
|
17 >>> with open(content_template, 'w') as file: |
|
18 ... _ = file.write('<div>Base template content</div>') |
|
19 |
|
20 >>> layout_template = os.path.join(temp_dir, 'layout-template.pt') |
|
21 >>> with open(layout_template, 'w') as file: |
|
22 ... _ = file.write(''' |
|
23 ... <html> |
|
24 ... <body> |
|
25 ... <div class="layout">${structure:view.render()}</div> |
|
26 ... </body> |
|
27 ... </html> |
|
28 ... ''') |
|
29 |
|
30 The templates must now be registered for a view and a request. We use the TemplateFactory directly |
|
31 here from *pyams_template* package, while it may be done using a *template_config* decorator: |
|
32 |
|
33 >>> from zope.interface import implementer, Interface |
|
34 >>> from pyramid.interfaces import IRequest |
|
35 >>> from pyams_template.interfaces import IContentTemplate, ILayoutTemplate |
|
36 |
|
37 >>> from pyams_template.template import TemplateFactory |
|
38 >>> factory = TemplateFactory(content_template, 'text/html') |
|
39 >>> config.registry.registerAdapter(factory, (Interface, IRequest), IContentTemplate) |
|
40 |
|
41 >>> factory = TemplateFactory(layout_template, 'text/html') |
|
42 >>> config.registry.registerAdapter(factory, (Interface, IRequest), ILayoutTemplate) |
|
43 |
|
44 Let's now create a pagelet view: |
|
45 |
|
46 >>> class IMyView(Interface): |
|
47 ... """View marker interface""" |
|
48 |
|
49 >>> from pyams_pagelet.pagelet import Pagelet |
|
50 >>> @implementer(IMyView) |
|
51 ... class MyView(Pagelet): |
|
52 ... """View class""" |
|
53 |
|
54 >>> from pyramid.testing import DummyRequest |
|
55 >>> content = object() |
|
56 >>> request = DummyRequest() |
|
57 >>> view = MyView(content, request) |
|
58 >>> print(view.render()) |
|
59 <div>Base template content</div> |
|
60 |
|
61 >>> print(view()) |
|
62 200 OK |
|
63 Content-Type: text/html; charset=UTF-8 |
|
64 Content-Length: 98 |
|
65 <BLANKLINE> |
|
66 <html> |
|
67 <body> |
|
68 <div class="layout"><div>Base template content</div></div> |
|
69 </body> |
|
70 </html> |
|
71 <BLANKLINE> |
|
72 |
|
73 But the standard way of using a pagelet is by using the "pagelet:" TALES expression: |
|
74 |
|
75 >>> pagelet_template = os.path.join(temp_dir, 'pagelet-template.pt') |
|
76 >>> with open(pagelet_template, 'w') as file: |
|
77 ... _ = file.write(''' |
|
78 ... <html> |
|
79 ... <body> |
|
80 ... <div class="pagelet">${structure:provider:pagelet}</div> |
|
81 ... </body> |
|
82 ... </html> |
|
83 ... ''') |
|
84 |
|
85 This template will be registered using the custom view interface: |
|
86 |
|
87 >>> from chameleon import PageTemplateFile |
|
88 >>> from pyams_viewlet.provider import ProviderExpr |
|
89 >>> PageTemplateFile.expression_types['provider'] = ProviderExpr |
|
90 |
|
91 >>> factory = TemplateFactory(pagelet_template, 'text/html') |
|
92 >>> config.registry.registerAdapter(factory, (IMyView, IRequest), ILayoutTemplate) |
|
93 |
|
94 >>> try: |
|
95 ... view() |
|
96 ... except Exception as e: |
|
97 ... print(repr(e)) |
|
98 ContentProviderLookupError('pagelet...) |
|
99 |
|
100 This exception is raised because the pagelet is not yet registered; this should be done |
|
101 automatically when *pyams_pagelet* package is included into Pyramid configuration: |
|
102 |
|
103 >>> from zope.contentprovider.interfaces import IContentProvider |
|
104 >>> from pyams_pagelet.interfaces import IPagelet |
|
105 >>> from pyams_pagelet.pagelet import PageletRenderer |
|
106 >>> config.registry.registerAdapter(PageletRenderer, |
|
107 ... (Interface, IRequest, IPagelet), |
|
108 ... IContentProvider, name='pagelet') |
|
109 >>> print(view()) |
|
110 200 OK |
|
111 Content-Type: text/html; charset=UTF-8 |
|
112 Content-Length: 99 |
|
113 <BLANKLINE> |
|
114 <html> |
|
115 <body> |
|
116 <div class="pagelet"><div>Base template content</div></div> |
|
117 </body> |
|
118 </html> |
|
119 <BLANKLINE> |
|
120 |
|
121 >>> tearDown() |