author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Fri, 30 Jan 2009 16:57:20 +0100 | |
changeset 543 | c0f2b6378f70 |
parent 318 | 6cb74102d611 |
child 1191 | 216141cf47a3 |
permissions | -rw-r--r-- |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
1 |
.. -*- coding: utf-8 -*- |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
2 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
3 |
.. _DefinitionVues: |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
4 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
5 |
Views definition |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
6 |
================ |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
7 |
|
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
8 |
This chapter aims to describe the concept of a `view` used all along |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
9 |
the development of a web application and how it has been implemented |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
10 |
in `CubicWeb`. |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
11 |
|
309
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
12 |
We'll start with a description of the interface providing you with a basic |
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
13 |
understanding of the classes and methods available, then detail the view |
316
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
14 |
selection principle which makes `CubicWeb` web interface very flexible. |
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
15 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
16 |
Basic class for views |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
17 |
--------------------- |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
18 |
|
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
19 |
Class `View` (`cubicweb.common.view`) |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
20 |
````````````````````````````````````` |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
21 |
|
309
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
22 |
This class is an abstraction of a view class, used as a base class for every |
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
23 |
renderable object such as views, templates, graphic components, etc. |
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
24 |
|
309
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
25 |
A `View` is instantiated to render a result set or part of a result set. `View` |
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
26 |
subclasses may be parametrized using the following class attributes: |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
27 |
|
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
28 |
* `templatable` indicates if the view may be embeded in a main |
309
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
29 |
template or if it has to be rendered standalone (i.e. XML views |
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
30 |
must not be embeded in the main template for HTML pages) |
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
31 |
* if the view is not templatable, it should set the `content_type` class |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
32 |
attribute to the correct MIME type (text/xhtml by default) |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
33 |
* the `category` attribute may be used in the interface to regroup related |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
34 |
objects together |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
35 |
|
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
36 |
At instantiation time, the standard `req`, `rset`, and `cursor` |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
37 |
attributes are added and the `w` attribute will be set at rendering |
309
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
38 |
time. |
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
39 |
|
309
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
40 |
A view writes to its output stream thanks to its attribute `w` (`UStreamIO`). |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
41 |
|
309
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
42 |
The basic interface for views is as follows (remember that the result set has a |
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
43 |
tabular structure with rows and columns, hence cells): |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
44 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
45 |
* `dispatch(**context)`, render the view by calling `call` or |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
46 |
`cell_call` depending on the given parameters |
309
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
47 |
* `call(**kwargs)`, call the view for a complete result set or null (default |
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
48 |
implementation calls `cell_call()` on each cell of the result set) |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
49 |
* `cell_call(row, col, **kwargs)`, call the view for a given cell of a result set |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
50 |
* `url()`, returns the URL enabling us to get the view with the current |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
51 |
result set |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
52 |
* `view(__vid, rset, __fallback_vid=None, **kwargs)`, call the view of identifier |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
53 |
`__vid` on the given result set. It is possible to give a view identifier |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
54 |
of fallback that will be used if the view requested is not applicable to the |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
55 |
result set |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
56 |
|
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
57 |
* `wview(__vid, rset, __fallback_vid=None, **kwargs)`, similar to `view` except |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
58 |
the flow is automatically passed in the parameters |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
59 |
|
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
60 |
* `html_headers()`, returns a list of HTML headers to set by the main template |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
61 |
|
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
62 |
* `page_title()`, returns the title to use in the HTML header `title` |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
63 |
|
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
64 |
* `creator(eid)`, returns the eid and the login of the entity creator of the entity |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
65 |
having the eid given in the parameter |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
66 |
|
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
67 |
Other basic views classes |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
68 |
````````````````````````` |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
69 |
Here are some of the subclasses of `View` defined in `cubicweb.common.view` |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
70 |
that are more concrete as they relates to data rendering within the application: |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
71 |
|
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
72 |
* `EntityView`, view applying to lines or cell containing an entity (e.g. an eid) |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
73 |
* `StartupView`, start view that does not require a result set to apply to |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
74 |
* `AnyRsetView`, view applied to any result set |
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
75 |
* `EmptyRsetView`, view applied to an empty result set |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
76 |
|
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
77 |
The selection view principle |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
78 |
---------------------------- |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
79 |
|
316
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
80 |
A view is essentially defined by: |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
81 |
|
306
1ed1da008e50
[doc] Removes references to LAX.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
301
diff
changeset
|
82 |
- an identifier (all objects in `CubicWeb` are entered in a registry |
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
83 |
and this identifier will be used as a key). This is defined in the class |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
84 |
attribute ``id``. |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
85 |
|
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
86 |
- a filter to select the resulsets it can be applied to. This is defined in |
316
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
87 |
the class attribute ``__selectors__``, which expects a tuple of selectors |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
88 |
as its value. |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
89 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
90 |
|
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
91 |
For a given identifier, multiple views can be defined. `CubicWeb` uses |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
92 |
a selector which computes scores so that it can identify and select the |
316
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
93 |
best view to apply in context. The selectors library is in |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
94 |
``cubicweb.common.selector`` and a library of the methods used to |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
95 |
compute scores is in ``cubicweb.vregistry.vreq``. |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
96 |
|
316
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
97 |
.. include:: B1021-views-selectors.en.txt |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
98 |
|
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
99 |
Registerer |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
100 |
`````````` |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
101 |
A view is also customizable through its attribute ``__registerer__``. |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
102 |
This is used at the time the application is launched to manage how |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
103 |
objects (views, graphic components, actions, etc.) |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
104 |
are registered in the `cubicWeb` registry. |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
105 |
|
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
106 |
A `registerer` can, for example, identify when we register an |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
107 |
object that is equivalent to an already registered object, which |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
108 |
could happen when we define two `primary` views for an entity type. |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
109 |
|
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
110 |
The purpose of a `registerer` is to control objects registry |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
111 |
at the application startup whereas `selectors` controls objects |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
112 |
when they are selected for display. |
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
113 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
114 |
|
306
1ed1da008e50
[doc] Removes references to LAX.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
301
diff
changeset
|
115 |
`CubicWeb` provides a lot of standard views for the default class |
1ed1da008e50
[doc] Removes references to LAX.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
301
diff
changeset
|
116 |
`EntityType`. You can find them in ``cubicweb/web/views/``. |
1ed1da008e50
[doc] Removes references to LAX.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
301
diff
changeset
|
117 |
|
316
99943acb44af
[doc] Add sub-section about selectors and a little about registerer in views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
309
diff
changeset
|
118 |
.. include:: B1022-views-stdlib.en.txt |
306
1ed1da008e50
[doc] Removes references to LAX.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
301
diff
changeset
|
119 |
|
318
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
120 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
121 |
Examples of views class |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
122 |
----------------------- |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
123 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
124 |
- Using the attribute `templatable` |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
125 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
126 |
:: |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
127 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
128 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
129 |
class RssView(XmlView): |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
130 |
id = 'rss' |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
131 |
title = _('rss') |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
132 |
templatable = False |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
133 |
content_type = 'text/xml' |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
134 |
http_cache_manager = MaxAgeHTTPCacheManager |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
135 |
cache_max_age = 60*60*2 # stay in http cache for 2 hours by default |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
136 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
137 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
138 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
139 |
- Using the attribute `__selectors__` |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
140 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
141 |
:: |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
142 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
143 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
144 |
class SearchForAssociationView(EntityView): |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
145 |
"""view called by the edition view when the user asks |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
146 |
to search for something to link to the edited eid |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
147 |
""" |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
148 |
id = 'search-associate' |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
149 |
title = _('search for association') |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
150 |
__selectors__ = (one_line_rset, match_search_state, accept_selector) |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
151 |
accepts = ('Any',) |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
152 |
search_states = ('linksearch',) |
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
153 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
154 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
155 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
156 |
|
6cb74102d611
[doc] Add a little to views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
316
diff
changeset
|
157 |
|
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
158 |
Example of a view customization |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
159 |
------------------------------- |
306
1ed1da008e50
[doc] Removes references to LAX.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
301
diff
changeset
|
160 |
|
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
161 |
We'll show you now an example of a ``primary`` view and how to customize it. |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
162 |
|
309
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
163 |
If you want to change the way a ``BlogEntry`` is displayed, just override |
7067c97cb182
[doc] minor text improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
308
diff
changeset
|
164 |
the method ``cell_call()`` of the view ``primary`` in ``BlogDemo/views.py`` :: |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
165 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
166 |
01. from ginco.web.views import baseviews |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
167 |
02. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
168 |
03. class BlogEntryPrimaryView(baseviews.PrimaryView): |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
169 |
04. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
170 |
05. accepts = ('BlogEntry',) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
171 |
06. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
172 |
07. def cell_call(self, row, col): |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
173 |
08. entity = self.entity(row, col) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
174 |
09. self.w(u'<h1>%s</h1>' % entity.title) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
175 |
10. self.w(u'<p>published on %s in category %s</p>' % \ |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
176 |
11. (entity.publish_date.strftime('%Y-%m-%d'), entity.category)) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
177 |
12. self.w(u'<p>%s</p>' % entity.text) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
178 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
179 |
The above source code defines a new primary view (`line 03`) for |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
180 |
``BlogEntry`` (`line 05`). |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
181 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
182 |
Since views are applied to resultsets and resulsets can be tables of |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
183 |
data, it is needed to recover the entity from its (row,col) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
184 |
coordinates (`line 08`). We will get to this in more detail later. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
185 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
186 |
The view has a ``self.w()`` method that is used to output data. Here `lines |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
187 |
09-12` output HTML tags and values of the entity's attributes. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
188 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
189 |
When displaying same blog entry as before, you will notice that the |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
190 |
page is now looking much nicer. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
191 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
192 |
.. image:: images/lax-book.09-new-view-blogentry.en.png |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
193 |
:alt: blog entries now look much nicer |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
194 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
195 |
Let us now improve the primary view of a blog :: |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
196 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
197 |
01. class BlogPrimaryView(baseviews.PrimaryView): |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
198 |
02. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
199 |
03. accepts = ('Blog',) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
200 |
04. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
201 |
05. def cell_call(self, row, col): |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
202 |
06. entity = self.entity(row, col) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
203 |
07. self.w(u'<h1>%s</h1>' % entity.title) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
204 |
08. self.w(u'<p>%s</p>' % entity.description) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
205 |
09. rset = self.req.execute('Any E WHERE E entry_of B, B eid "%s"' % entity.eid) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
206 |
10. self.wview('primary', rset) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
207 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
208 |
In the above source code, `lines 01-08` are similar to the previous |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
209 |
view we defined. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
210 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
211 |
At `line 09`, a simple request in made to build a resultset with all |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
212 |
the entities linked to the current ``Blog`` entity by the relationship |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
213 |
``entry_of``. The part of the framework handling the request knows |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
214 |
about the schema and infer that such entities have to be of the |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
215 |
``BlogEntry`` kind and retrieves them. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
216 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
217 |
The request returns a selection of data called a resultset. At |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
218 |
`line 10` the view 'primary' is applied to this resultset to output |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
219 |
HTML. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
220 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
221 |
**This is to be compared to interfaces and protocols in object-oriented |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
222 |
languages. Applying a given view to all the entities of a resultset only |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
223 |
requires the availability, for each entity of this resultset, of a |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
224 |
view with that name that can accepts the entity.** |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
225 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
226 |
Assuming we added entries to the blog titled `MyLife`, displaying it |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
227 |
now allows to read its description and all its entries. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
228 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
229 |
.. image:: images/lax-book.10-blog-with-two-entries.en.png |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
230 |
:alt: a blog and all its entries |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
231 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
232 |
**Before we move forward, remember that the selection/view principle is |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
233 |
at the core of `CubicWeb`. Everywhere in the engine, data is requested |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
234 |
using the RQL language, then HTML/XML/text/PNG is output by applying a |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
235 |
view to the resultset returned by the query. That is where most of the |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
236 |
flexibility comes from.** |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
237 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
238 |
[WRITE ME] |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
239 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
240 |
* implementing interfaces, calendar for blog entries |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
241 |
* show that a calendar view can export data to ical |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
242 |
|
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
243 |
We will implement the `cubicweb.interfaces.ICalendarable` interfaces on |
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
244 |
entities.BlogEntry and apply the OneMonthCalendar and iCalendar views |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
245 |
to resultsets like "Any E WHERE E is BlogEntry" |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
246 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
247 |
* create view "blogentry table" with title, publish_date, category |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
248 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
249 |
We will show that by default the view that displays |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
250 |
"Any E,D,C WHERE E publish_date D, E category C" is the table view. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
251 |
Of course, the same can be obtained by calling |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
252 |
self.wview('table',rset) |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
253 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
254 |
* in view blog, select blogentries and apply view "blogentry table" |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
255 |
* demo ajax by filtering blogentry table on category |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
256 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
257 |
we did the same with 'primary', but with tables we can turn on filters |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
258 |
and show that ajax comes for free. |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
259 |
[FILLME] |
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
260 |
|
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
261 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
262 |
Templates |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
263 |
--------- |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
264 |
|
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
265 |
*Templates* are specific view that does not depend on a result set. The basic |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
266 |
class `Template` (`cubicweb.common.view`) is derived from the class `View`. |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
267 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
268 |
To build a HTML page, a *main template* is used. In general, the template of |
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
269 |
identifier `main` is the one to use (it is not used in case an error is raised or for |
229
767ff7f5d5a7
[doc] Replace all : s/by example/for example/
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
127
diff
changeset
|
270 |
the login form for example). This template uses other templates in addition |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
271 |
to the views which depends on the content to generate the HTML page to return. |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
272 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
273 |
A *template* is responsible for: |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
274 |
|
301 | 275 |
1. executing RQL query of data to render if necessary |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
276 |
2. identifying the view to use to render data if it is not specified |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
277 |
3. composing the HTML page to return |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
278 |
|
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
279 |
You will find out more about templates in :ref:`templates`. |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
280 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
281 |
XML views, binaries... |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
282 |
---------------------- |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
283 |
For the views generating other formats that HTML (an image generated dynamically |
229
767ff7f5d5a7
[doc] Replace all : s/by example/for example/
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
127
diff
changeset
|
284 |
for example), and which can not usually be included in the HTML page generated |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
285 |
by the main template (see above), you have to: |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
286 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
287 |
* set the atribute `templatable` of the class to `False` |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
288 |
* set, through the attribute `content_type` of the class, the MIME type generated |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
289 |
by the view to `application/octet-stream` |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
290 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
291 |
For the views dedicated to binary content creation (an image dynamically generated |
229
767ff7f5d5a7
[doc] Replace all : s/by example/for example/
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
127
diff
changeset
|
292 |
for example), we have to set the attribute `binary` of the class to `True` (which |
308
73a352526577
[doc] Improvement to the Views chapter. Moved content about the main template in the Templates chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
306
diff
changeset
|
293 |
implies that `templatable == False`, so that the attribute `w` of the view could be |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
294 |
replaced by a binary flow instead of unicode). |
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff
changeset
|
295 |
|
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
296 |
(X)HTML tricks to apply |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
297 |
----------------------- |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
298 |
|
229
767ff7f5d5a7
[doc] Replace all : s/by example/for example/
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
127
diff
changeset
|
299 |
Some web browser (Firefox for example) are not happy with empty `<div>` |
102
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
300 |
(by empty we mean that there is no content in the tag, but there |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
301 |
could be attributes), so we should always use `<div></div>` even if |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
302 |
it is empty and not use `<div/>`. |
ef84f6b2e4cf
[doc] Initial translation of views chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
93
diff
changeset
|
303 |