|
1 ====================== |
|
2 pyams_template package |
|
3 ====================== |
|
4 |
|
5 This doctests are based on z3c.template doctests. |
|
6 |
|
7 First let's show how to produce content from a view: |
|
8 |
|
9 >>> from pyramid.testing import setUp, tearDown |
|
10 >>> config = setUp() |
|
11 |
|
12 >>> import os, tempfile |
|
13 >>> temp_dir = tempfile.mkdtemp() |
|
14 |
|
15 >>> content_template = os.path.join(temp_dir, 'content-template.pt') |
|
16 >>> with open(content_template, 'w') as file: |
|
17 ... _ = file.write('<div>Base template content</div>') |
|
18 |
|
19 We can now register a view class implementing an interface: |
|
20 |
|
21 >>> from zope.interface import implementer, Interface |
|
22 >>> from pyams_template.interfaces import IContentTemplate, IPageTemplate |
|
23 |
|
24 >>> class IMyView(Interface): |
|
25 ... """View marker interface""" |
|
26 |
|
27 >>> @implementer(IMyView) |
|
28 ... class MyView: |
|
29 ... template = None |
|
30 ... def __init__(self, context, request): |
|
31 ... self.context = context |
|
32 ... self.request = request |
|
33 ... def __call__(self): |
|
34 ... if self.template is None: |
|
35 ... template = config.registry.getMultiAdapter((self, self.request), |
|
36 ... IContentTemplate) |
|
37 ... return template() |
|
38 ... return self.template() |
|
39 |
|
40 Let's now call the view and check the output: |
|
41 |
|
42 >>> from pyramid.testing import DummyRequest |
|
43 >>> root = object() |
|
44 >>> request = DummyRequest() |
|
45 >>> view = MyView(root, request) |
|
46 |
|
47 Since the template is not yet registered, rendering may fail: |
|
48 |
|
49 >>> view() |
|
50 Traceback (most recent call last): |
|
51 ... |
|
52 zope.interface.interfaces.ComponentLookupError: ... |
|
53 |
|
54 So let's now register the template; this is commonly done using "template_config" decorator, or |
|
55 using ZCML: |
|
56 |
|
57 >>> from pyams_template.template import TemplateFactory |
|
58 >>> factory = TemplateFactory(content_template, 'text/html') |
|
59 >>> factory |
|
60 <pyams_template.template.TemplateFactory object at ...> |
|
61 |
|
62 >>> from pyramid.interfaces import IRequest |
|
63 >>> config.registry.registerAdapter(factory, (Interface, IRequest), IContentTemplate) |
|
64 >>> config.registry.getMultiAdapter((view, request), IPageTemplate) |
|
65 <PyramidPageTemplateFile .../content-template.pt> |
|
66 |
|
67 >>> print(view()) |
|
68 <div>Base template content</div> |
|
69 |
|
70 Now we can register another template for this view: |
|
71 |
|
72 >>> my_template = os.path.join(temp_dir, 'my-template.pt') |
|
73 >>> with open(my_template, 'w') as file: |
|
74 ... _ = file.write('<div>This is my template</div>') |
|
75 >>> factory = TemplateFactory(my_template, 'text/html') |
|
76 >>> config.registry.registerAdapter(factory, (IMyView, IRequest), IContentTemplate) |
|
77 >>> print(view()) |
|
78 <div>This is my template</div> |
|
79 |
|
80 It's generally easier to use a decorator to register templates: |
|
81 |
|
82 >>> from pyramid.view import view_config |
|
83 >>> from pyams_template.template import template_config |
|
84 |
|
85 >>> my_other_template = os.path.join(temp_dir, 'my-other-template.pt') |
|
86 >>> with open(my_other_template, 'w') as file: |
|
87 ... _ = file.write('<div>This is my template for view 2</div>') |
|
88 |
|
89 >>> @template_config(template=my_other_template) |
|
90 ... class MyView2(MyView): |
|
91 ... """Simple view subclass""" |
|
92 |
|
93 In testing mode we always have to register template manually because venusian can't scan test |
|
94 unit: |
|
95 |
|
96 >>> factory = TemplateFactory(my_other_template, 'text/html') |
|
97 >>> config.registry.registerAdapter(factory, (MyView2, IRequest), IContentTemplate) |
|
98 |
|
99 >>> view = MyView2(root, request) |
|
100 >>> print(view()) |
|
101 <div>This is my template for view 2</div> |
|
102 |
|
103 >>> tearDown() |