|
1 .. -*- coding: utf-8 -*- |
|
2 |
|
3 .. _templates: |
|
4 |
|
5 Templates |
|
6 ========= |
|
7 |
|
8 [WRITE ME] |
|
9 |
|
10 * talk about main templates, etc. |
|
11 |
|
12 |
|
13 |
|
14 Look at ``cubicweb/web/views/basetemplates.py`` and you will |
|
15 find the base templates used to generate HTML for your application. |
|
16 |
|
17 A page is composed as indicated on the schema below : |
|
18 |
|
19 .. image:: ../../../images/lax-book.06-main-template-layout.en.png |
|
20 |
|
21 In this section we will go through a couple of the primary templates |
|
22 you must be interested in, that is to say, the HTMLPageHeader, |
|
23 the HTMLPageFooter and the TheMainTemplate. |
|
24 |
|
25 |
|
26 HTMLPageHeader |
|
27 -------------- |
|
28 |
|
29 Customize header |
|
30 ~~~~~~~~~~~~~~~~ |
|
31 |
|
32 Let's now move the search box in the header and remove the login form |
|
33 from the header. We'll show how to move it to the left column of the application. |
|
34 |
|
35 Let's say we do not want anymore the login menu to be in the header, but we |
|
36 prefer it to be in the left column just below the logo. As the left column is |
|
37 rendered by ``TheMainTemplate``, we will show how to do it in TheMainTemplate_. |
|
38 |
|
39 First, to remove the login menu, we just need to comment out the display of the |
|
40 login component such as follows : :: |
|
41 |
|
42 class MyHTMLPageHeader(HTMLPageHeader): |
|
43 |
|
44 def main_header(self, view): |
|
45 """build the top menu with authentification info and the rql box""" |
|
46 self.w(u'<table id="header"><tr>\n') |
|
47 self.w(u'<td id="firstcolumn">') |
|
48 self._cw.vreg.select_component('logo', self._cw, self.cw_rset).dispatch(w=self.w) |
|
49 self.w(u'</td>\n') |
|
50 # appliname and breadcrumbs |
|
51 self.w(u'<td id="headtext">') |
|
52 comp = self._cw.vreg.select_component('appliname', self._cw, self.cw_rset) |
|
53 if comp and comp.propval('visible'): |
|
54 comp.dispatch(w=self.w) |
|
55 comp = self._cw.vreg.select_component('breadcrumbs', self._cw, self.cw_rset, view=view) |
|
56 if comp and comp.propval('visible'): |
|
57 comp.dispatch(w=self.w, view=view) |
|
58 self.w(u'</td>') |
|
59 # logged user and help |
|
60 #self.w(u'<td>\n') |
|
61 #comp = self._cw.vreg.select_component('loggeduserlink', self._cw, self.cw_rset) |
|
62 #comp.dispatch(w=self.w) |
|
63 #self.w(u'</td><td>') |
|
64 |
|
65 self.w(u'<td>') |
|
66 helpcomp = self._cw.vreg.select_component('help', self._cw, self.cw_rset) |
|
67 if helpcomp: # may not be available if Card is not defined in the schema |
|
68 helpcomp.dispatch(w=self.w) |
|
69 self.w(u'</td>') |
|
70 # lastcolumn |
|
71 self.w(u'<td id="lastcolumn">') |
|
72 self.w(u'</td>\n') |
|
73 self.w(u'</tr></table>\n') |
|
74 self.template('logform', rset=self.cw_rset, id='popupLoginBox', klass='hidden', |
|
75 title=False, message=False) |
|
76 |
|
77 |
|
78 |
|
79 .. image:: ../../../images/lax-book.06-header-no-login.en.png |
|
80 |
|
81 Let's now move the search box in the top-right header area. To do so, we will |
|
82 first create a method to get the search box display and insert it in the header |
|
83 table. |
|
84 |
|
85 :: |
|
86 |
|
87 from cubicweb.web.views.basetemplates import HTMLPageHeader |
|
88 class MyHTMLPageHeader(HTMLPageHeader): |
|
89 def main_header(self, view): |
|
90 """build the top menu with authentification info and the rql box""" |
|
91 self.w(u'<table id="header"><tr>\n') |
|
92 self.w(u'<td id="firstcolumn">') |
|
93 self._cw.vreg.select_component('logo', self._cw, self.cw_rset).dispatch(w=self.w) |
|
94 self.w(u'</td>\n') |
|
95 # appliname and breadcrumbs |
|
96 self.w(u'<td id="headtext">') |
|
97 comp = self._cw.vreg.select_component('appliname', self._cw, self.cw_rset) |
|
98 if comp and comp.propval('visible'): |
|
99 comp.dispatch(w=self.w) |
|
100 comp = self._cw.vreg.select_component('breadcrumbs', self._cw, self.cw_rset, view=view) |
|
101 if comp and comp.propval('visible'): |
|
102 comp.dispatch(w=self.w, view=view) |
|
103 self.w(u'</td>') |
|
104 |
|
105 # logged user and help |
|
106 #self.w(u'<td>\n') |
|
107 #comp = self._cw.vreg.select_component('loggeduserlink', self._cw, self.cw_rset) |
|
108 #comp.dispatch(w=self.w) |
|
109 #self.w(u'</td><td>') |
|
110 |
|
111 # search box |
|
112 self.w(u'<td>') |
|
113 self.get_searchbox(view, 'left') |
|
114 self.w(u'</td>') |
|
115 |
|
116 self.w(u'<td>') |
|
117 helpcomp = self._cw.vreg.select_component('help', self._cw, self.cw_rset) |
|
118 if helpcomp: # may not be available if Card is not defined in the schema |
|
119 helpcomp.dispatch(w=self.w) |
|
120 self.w(u'</td>') |
|
121 # lastcolumn |
|
122 self.w(u'<td id="lastcolumn">') |
|
123 self.w(u'</td>\n') |
|
124 self.w(u'</tr></table>\n') |
|
125 self.template('logform', rset=self.cw_rset, id='popupLoginBox', klass='hidden', |
|
126 title=False, message=False) |
|
127 |
|
128 def get_searchbox(self, view, context): |
|
129 boxes = list(self._cw.vreg.poss_visible_objects('boxes', self._cw, self.cw_rset, |
|
130 view=view, context=context)) |
|
131 if boxes: |
|
132 for box in boxes: |
|
133 if box.__regid__ == 'search_box': |
|
134 box.dispatch(w=self.w, view=view) |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 HTMLPageFooter |
|
140 -------------- |
|
141 |
|
142 If you want to change the footer for example, look |
|
143 for HTMLPageFooter and override it in your views file as in : |
|
144 :: |
|
145 |
|
146 form cubicweb.web.views.basetemplates import HTMLPageFooter |
|
147 class MyHTMLPageFooter(HTMLPageFooter): |
|
148 def call(self, **kwargs): |
|
149 self.w(u'<div class="footer">') |
|
150 self.w(u'This website has been created with <a href="http://cubicweb.org">CubicWeb</a>.') |
|
151 self.w(u'</div>') |
|
152 |
|
153 Updating a view does not require any restart of the server. By reloading |
|
154 the page you can see your new page footer. |
|
155 |
|
156 |
|
157 TheMainTemplate |
|
158 --------------- |
|
159 .. _TheMainTemplate: |
|
160 |
|
161 TheMainTemplate is responsible for the general layout of the entire application. |
|
162 It defines the template of ``__regid__ = main`` that is used by the instance. |
|
163 |
|
164 The default main template (`cubicweb.web.views.basetemplates.TheMainTemplate`) |
|
165 builds the page based on the following pattern: |
|
166 |
|
167 .. image:: ../../../images/main_template_layout.png |
|
168 |
|
169 The rectangle containing `view.dispatch()` represents the area where the content |
|
170 view has to be displayed. The others represents sub-templates called to complete |
|
171 the page. A default implementation of those is provided in |
|
172 `cubicweb.views.basetemplates`. You can, of course, overload those sub-templates |
|
173 to implement your own customization of the HTML page. |
|
174 |
|
175 We can also control certain aspects of the main template thanks to the following |
|
176 forms parameters: |
|
177 |
|
178 * `__notemplate`, if present (whatever the value assigned), only the content view |
|
179 is returned |
|
180 * `__force_display`, if present and its value is not null, no navigation |
|
181 whatever the number of entities to display |
|
182 * `__method`, if the result set to render contains only one entity and this |
|
183 parameter is set, it refers to a method to call on the entity by passing it |
|
184 the dictionary of the forms parameters, before going the classic way (through |
|
185 step 1 and 2 described juste above) |
|
186 |
|
187 The MainTemplate is a bit complex as it tries to accomodate many |
|
188 different cases. We are now about to go through it and cutomize entirely |
|
189 our application. |