3 .. _DefinitionVues: |
3 .. _DefinitionVues: |
4 |
4 |
5 Views definition |
5 Views definition |
6 ================ |
6 ================ |
7 |
7 |
|
8 This chapter aims to describe the concept of a `view` used all along |
|
9 the development of a web application and how it has been implemented |
|
10 in `CubicWeb`. |
|
11 |
|
12 We'll start with a description of the interface providing you a basic |
|
13 understanding of the classes and methods available. We'll also |
|
14 take time to develop the view selection principle we implemented |
|
15 which is very unique to `CubicWeb` and makes it powerfull. |
|
16 |
8 Basic class for views |
17 Basic class for views |
9 --------------------- |
18 --------------------- |
10 |
19 |
11 Class `View` (`cubicweb.common.view`) |
20 Class `View` (`cubicweb.common.view`) |
12 ````````````````````````````````````` |
21 ````````````````````````````````````` |
13 |
22 |
|
23 This class is an abstraction of a view class, used as a base for every |
|
24 renderable object such as views, templates, graphic components... web. |
|
25 |
|
26 A `View` is instantiated to render a or part of a result set. `View` |
|
27 subclasses may be parametrized using the following class attributes: |
|
28 |
|
29 * `templatable` indicates if the view may be embeded in a main |
|
30 template or if it has to be rendered standalone (i.e. XML for |
|
31 instance) |
|
32 * if the view is not templatable, it should set the `content_type` class |
|
33 attribute to the correct MIME type (text/xhtml by default) |
|
34 * the `category` attribute may be used in the interface to regroup related |
|
35 objects together |
|
36 |
|
37 At instantiation time, the standard `req`, `rset`, and `cursor` |
|
38 attributes are added and the `w` attribute will be set at rendering |
|
39 time to a write function to use. |
|
40 |
14 A view writes in its output exit thanks to its attribute `w` (`UStreamIO`). |
41 A view writes in its output exit thanks to its attribute `w` (`UStreamIO`). |
15 |
42 |
16 The basic interface for views is as follows: |
43 The basic interface for views is as follows: |
17 |
44 |
18 * `dispatch(**context)`, render the view by calling `call` or |
45 * `dispatch(**context)`, render the view by calling `call` or |
19 `cell_call` depending on the given parameters |
46 `cell_call` depending on the given parameters |
20 * `call(**kwargs)`, call the view for a complete result set or null |
47 * `call(**kwargs)`, call the view for a complete result set or null and call |
|
48 the method `cell_call()` on each cell of the result set |
21 * `cell_call(row, col, **kwargs)`, call the view for a given cell of a result set |
49 * `cell_call(row, col, **kwargs)`, call the view for a given cell of a result set |
22 * `url()`, returns the URL enabling us to get the view with the current |
50 * `url()`, returns the URL enabling us to get the view with the current |
23 result set |
51 result set |
24 * `view(__vid, rset, __fallback_vid=None, **kwargs)`, call the view of identifier |
52 * `view(__vid, rset, __fallback_vid=None, **kwargs)`, call the view of identifier |
25 `__vid` on the given result set. It is possible to give a view identifier |
53 `__vid` on the given result set. It is possible to give a view identifier |
34 * `page_title()`, returns the title to use in the HTML header `title` |
62 * `page_title()`, returns the title to use in the HTML header `title` |
35 |
63 |
36 * `creator(eid)`, returns the eid and the login of the entity creator of the entity |
64 * `creator(eid)`, returns the eid and the login of the entity creator of the entity |
37 having the eid given in the parameter |
65 having the eid given in the parameter |
38 |
66 |
39 Other basic classes: |
67 Other basic views classes |
|
68 ````````````````````````` |
|
69 Here are some of the subclasses of `View` defined in `cubicweb.common.view` |
|
70 that are more concrete as they relates to data rendering within the application: |
40 |
71 |
41 * `EntityView`, view applying to lines or cell containing an entity (e.g. an eid) |
72 * `EntityView`, view applying to lines or cell containing an entity (e.g. an eid) |
42 * `StartupView`, start view that does not require a result set to apply to |
73 * `StartupView`, start view that does not require a result set to apply to |
43 * `AnyRsetView`, view applied to any result set |
74 * `AnyRsetView`, view applied to any result set |
44 |
75 * `EmptyRsetView`, view applied to an empty result set |
45 |
76 |
46 The selection view principle |
77 The selection view principle |
47 ---------------------------- |
78 ---------------------------- |
48 |
79 |
49 A view includes : |
80 A view includes : |
50 |
81 |
51 - an identifier (all objects in `CubicWeb` are entered in a registry |
82 - an identifier (all objects in `CubicWeb` are entered in a registry |
52 and this identifier will be used as a key) |
83 and this identifier will be used as a key). This is defined in the class |
|
84 attribute ``id``. |
53 |
85 |
54 - a filter to select the resulsets it can be applied to |
86 - a filter to select the resulsets it can be applied to. This is defined in |
|
87 the class attribute ``__selectors__`` |
55 |
88 |
56 |
89 |
57 For a given identifier, multiple views can be defined. `CubicWeb` uses |
90 For a given identifier, multiple views can be defined. `CubicWeb` uses |
58 a selector which computes scores so that it can identify and select the |
91 a selector which computes scores so that it can identify and select the |
59 best view to apply in context. The selector library is in |
92 best view to apply in context. The selector library is in |
60 ``cubicweb.common.selector`` and a library of the methods used to |
93 ``cubicweb.common.selector`` and a library of the methods used to |
61 compute scores is in ``cubicweb.vregistry.vreq``. |
94 compute scores is in ``cubicweb.vregistry.vreq``. |
62 |
95 |
63 |
96 |
64 `CubicWeb` provides a lot of standard views, for a complete list, you |
|
65 will have to read the code in directory ``cubicweb/web/views/`` (XXX |
|
66 improve doc). |
|
67 |
|
68 `CubicWeb` provides a lot of standard views for the default class |
97 `CubicWeb` provides a lot of standard views for the default class |
69 `EntityType`. You can find them in ``cubicweb/web/views/``. |
98 `EntityType`. You can find them in ``cubicweb/web/views/``. |
70 |
99 |
71 The basic views defined are : |
100 .. include:: B1021-views-stdlib.en.txt |
72 |
101 |
73 *null* |
102 |
74 |
103 Example of a view customization |
75 |
104 ------------------------------- |
76 |
105 |
77 |
106 We'll show you now an example of a ``primary`` view and how to customize it. |
78 |
|
79 For example, the view named ``primary`` is the one used to display |
|
80 a single entity. |
|
81 |
107 |
82 If you want to change the way a ``BlogEntry`` is displayed, just |
108 If you want to change the way a ``BlogEntry`` is displayed, just |
83 override the view ``primary`` in ``BlogDemo/views.py`` :: |
109 override the method ``cell_call()`` of the view ``primary`` in ``BlogDemo/views.py`` :: |
84 |
110 |
85 01. from ginco.web.views import baseviews |
111 01. from ginco.web.views import baseviews |
86 02. |
112 02. |
87 03. class BlogEntryPrimaryView(baseviews.PrimaryView): |
113 03. class BlogEntryPrimaryView(baseviews.PrimaryView): |
88 04. |
114 04. |
157 [WRITE ME] |
183 [WRITE ME] |
158 |
184 |
159 * implementing interfaces, calendar for blog entries |
185 * implementing interfaces, calendar for blog entries |
160 * show that a calendar view can export data to ical |
186 * show that a calendar view can export data to ical |
161 |
187 |
162 We will implement the cubicwweb.interfaces.ICalendarable interfaces on |
188 We will implement the `cubicweb.interfaces.ICalendarable` interfaces on |
163 entities.BloEntry and apply the OneMonthCalendar and iCalendar views |
189 entities.BlogEntry and apply the OneMonthCalendar and iCalendar views |
164 to resultsets like "Any E WHERE E is BlogEntry" |
190 to resultsets like "Any E WHERE E is BlogEntry" |
165 |
191 |
166 * create view "blogentry table" with title, publish_date, category |
192 * create view "blogentry table" with title, publish_date, category |
167 |
193 |
168 We will show that by default the view that displays |
194 We will show that by default the view that displays |
183 |
209 |
184 *Templates* are specific view that does not depend on a result set. The basic |
210 *Templates* are specific view that does not depend on a result set. The basic |
185 class `Template` (`cubicweb.common.view`) is derived from the class `View`. |
211 class `Template` (`cubicweb.common.view`) is derived from the class `View`. |
186 |
212 |
187 To build a HTML page, a *main template* is used. In general, the template of |
213 To build a HTML page, a *main template* is used. In general, the template of |
188 identifier `main` is the one (it is not used in case an error is raised or for |
214 identifier `main` is the one to use (it is not used in case an error is raised or for |
189 the login form for example). This template uses other templates in addition |
215 the login form for example). This template uses other templates in addition |
190 to the views which depends on the content to generate the HTML page to return. |
216 to the views which depends on the content to generate the HTML page to return. |
191 |
217 |
192 A *template* is responsible for: |
218 A *template* is responsible for: |
193 |
219 |
194 1. executing RQL query of data to render if necessary |
220 1. executing RQL query of data to render if necessary |
195 2. identifying the view to use to render data if it is not specified |
221 2. identifying the view to use to render data if it is not specified |
196 3. composing the HTML page to return |
222 3. composing the HTML page to return |
197 |
223 |
198 |
224 You will find out more about templates in :ref:`templates`. |
199 The default main template (`cubicweb.web.views.basetemplates.TheMainTemplate`) |
|
200 ------------------------------------------------------------------------------ |
|
201 |
|
202 The default main template build the page based on the following pattern: |
|
203 |
|
204 .. image:: images/main_template_layout.png |
|
205 |
|
206 The rectangle containing `view.dispathc()` represents the area where the content |
|
207 view has to be displayed. The others represents sub-templates called to complete |
|
208 the page. A default implementation of those is provided in |
|
209 `cubicweb.views.basetemplates`. You can, of course, overload those sub-templates |
|
210 to implement your own customization of the HTML page. |
|
211 |
|
212 We can also control certain aspects of the main template thanks to the following |
|
213 forms parameters: |
|
214 |
|
215 * `__notemplate`, if present (whatever the value assigned), only the content view |
|
216 is returned |
|
217 * `__force_display`, if present and its value is not null, no navigation |
|
218 whatever the number of entities to display |
|
219 * `__method`, if the result set to render contains only one entity and this |
|
220 parameter is set, it refers to a method to call on the entity by passing it |
|
221 the dictionnary of the forms parameters, before going the classic way (through |
|
222 step 1 and 2 described juste above) |
|
223 |
|
224 .. include:: B1021-views-stdlib.en.txt |
|
225 |
|
226 |
225 |
227 XML views, binaries... |
226 XML views, binaries... |
228 ---------------------- |
227 ---------------------- |
229 For the views generating other formats that HTML (an image generated dynamically |
228 For the views generating other formats that HTML (an image generated dynamically |
230 for example), and which can not usually be included in the HTML page generated |
229 for example), and which can not usually be included in the HTML page generated |
234 * set, through the attribute `content_type` of the class, the MIME type generated |
233 * set, through the attribute `content_type` of the class, the MIME type generated |
235 by the view to `application/octet-stream` |
234 by the view to `application/octet-stream` |
236 |
235 |
237 For the views dedicated to binary content creation (an image dynamically generated |
236 For the views dedicated to binary content creation (an image dynamically generated |
238 for example), we have to set the attribute `binary` of the class to `True` (which |
237 for example), we have to set the attribute `binary` of the class to `True` (which |
239 implies that `templateable == False`, so that the attribute `w` of the view could be |
238 implies that `templatable == False`, so that the attribute `w` of the view could be |
240 replaced by a binary flow instead of unicode). |
239 replaced by a binary flow instead of unicode). |
241 |
240 |
242 (X)HTML tricks to apply |
241 (X)HTML tricks to apply |
243 ----------------------- |
242 ----------------------- |
244 |
243 |