1 .. -*- coding: utf-8 -*- |
|
2 |
|
3 Templates |
|
4 --------- |
|
5 |
|
6 Look at ``cubicweb/web/views/basetemplates.py`` and you will |
|
7 find the base templates used to generate HTML for your application. |
|
8 |
|
9 A page is composed as indicated on the schema below: |
|
10 |
|
11 .. image:: images/lax-book.06-main-template-layout.en.png |
|
12 |
|
13 In this section we will demonstrate a change in one of the main |
|
14 interesting template from the three you will look for, |
|
15 that is to say, the HTMLPageHeader, the HTMLPageFooter |
|
16 and the TheMainTemplate. |
|
17 |
|
18 |
|
19 Customize a template |
|
20 ~~~~~~~~~~~~~~~~~~~~ |
|
21 |
|
22 Based on the diagram below, each template can be overriden |
|
23 by your customized template. To do so, we recommand you create |
|
24 a Python module ``blog.views.templates`` to keep it organized. |
|
25 In this module you will have to import the parent class you are |
|
26 interested as follows: :: |
|
27 |
|
28 from cubicweb.web.views.basetemplates import HTMLPageHeader, \ |
|
29 HTMLPageFooter, TheMainTemplate |
|
30 |
|
31 and then create your sub-class:: |
|
32 |
|
33 class MyBlogHTMLPageHeader(HTMLPageHeader): |
|
34 ... |
|
35 |
|
36 Customize header |
|
37 ````````````````` |
|
38 |
|
39 Let's now move the search box in the header and remove the login form |
|
40 from the header. We'll show how to move it to the left column of the application. |
|
41 |
|
42 Let's say we do not want anymore the login menu to be in the header |
|
43 |
|
44 First, to remove the login menu, we just need to comment out the display of the |
|
45 login graphic component such as follows: :: |
|
46 |
|
47 class MyBlogHTMLPageHeader(HTMLPageHeader): |
|
48 |
|
49 def main_header(self, view): |
|
50 """build the top menu with authentification info and the rql box""" |
|
51 self.w(u'<table id="header"><tr>\n') |
|
52 self.w(u'<td id="firstcolumn">') |
|
53 self.vreg.select_component('logo', self.req, self.rset).dispatch(w=self.w) |
|
54 self.w(u'</td>\n') |
|
55 # appliname and breadcrumbs |
|
56 self.w(u'<td id="headtext">') |
|
57 comp = self.vreg.select_component('appliname', self.req, self.rset) |
|
58 if comp and comp.propval('visible'): |
|
59 comp.dispatch(w=self.w) |
|
60 comp = self.vreg.select_component('breadcrumbs', self.req, self.rset, view=view) |
|
61 if comp and comp.propval('visible'): |
|
62 comp.dispatch(w=self.w, view=view) |
|
63 self.w(u'</td>') |
|
64 # logged user and help |
|
65 #self.w(u'<td>\n') |
|
66 #comp = self.vreg.select_component('loggeduserlink', self.req, self.rset) |
|
67 #comp.dispatch(w=self.w) |
|
68 #self.w(u'</td><td>') |
|
69 |
|
70 self.w(u'<td>') |
|
71 helpcomp = self.vreg.select_component('help', self.req, self.rset) |
|
72 if helpcomp: # may not be available if Card is not defined in the schema |
|
73 helpcomp.dispatch(w=self.w) |
|
74 self.w(u'</td>') |
|
75 # lastcolumn |
|
76 self.w(u'<td id="lastcolumn">') |
|
77 self.w(u'</td>\n') |
|
78 self.w(u'</tr></table>\n') |
|
79 self.template('logform', rset=self.rset, id='popupLoginBox', klass='hidden', |
|
80 title=False, message=False) |
|
81 |
|
82 |
|
83 |
|
84 .. image:: images/lax-book.06-header-no-login.en.png |
|
85 |
|
86 Customize footer |
|
87 ```````````````` |
|
88 |
|
89 If you want to change the footer for example, look |
|
90 for HTMLPageFooter and override it in your views file as in: :: |
|
91 |
|
92 from cubicweb.web.views.basetemplates import HTMLPageFooter |
|
93 |
|
94 class MyHTMLPageFooter(HTMLPageFooter): |
|
95 |
|
96 def call(self, **kwargs): |
|
97 self.w(u'<div class="footer">') |
|
98 self.w(u'This website has been created with <a href="http://cubicweb.org">CubicWeb</a>.') |
|
99 self.w(u'</div>') |
|
100 |
|
101 Updating a view does not require any restart of the server. By reloading |
|
102 the page you can see your new page footer. |
|
103 |
|
104 |
|
105 TheMainTemplate |
|
106 ``````````````` |
|
107 |
|
108 .. _TheMainTemplate: |
|
109 |
|
110 The MainTemplate is a bit complex as it tries to accomodate many |
|
111 different cases. We are now about to go through it and cutomize entirely |
|
112 our application. |
|
113 |
|
114 TheMainTemplate is responsible for the general layout of the entire application. |
|
115 It defines the template of ``id = main`` that is used by the application. Is |
|
116 also defined in ``cubicweb/web/views/basetemplates.py`` another template that can |
|
117 be used based on TheMainTemplate called SimpleMainTemplate which does not have |
|
118 a top section. |
|
119 |
|
120 .. image:: images/lax-book.06-simple-main-template.en.png |
|
121 |
|
122 XXX |
|
123 [WRITE ME] |
|
124 |
|
125 * customize MainTemplate and show that everything in the user |
|
126 interface can be changed |
|
127 |
|