Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 42f69828

Von Sven Schöling vor mehr als 11 Jahren hinzugefügt

Lange überfällige Doku zu Layouts

Unterschiede anzeigen:

SL/Layout/Base.pm
160 160
}
161 161

  
162 162
1;
163

  
164
__END__
165

  
166
=encoding utf-8
167

  
168
=head1 NAME
169

  
170
SL::Layout::Base - Base class for layouts
171

  
172
=head1 SYNOPSIS
173

  
174
  package SL::Layout::MyLayout;
175

  
176
  use parent qw(SL::Layout::Base);
177

  
178
=head1 DESCRIPTION
179

  
180
For a description about the external interface of layouts in general see
181
L<SL::Layout::Dispatcher>.
182

  
183
This is a base class for layouts in general. It provides the basic interface
184
and some capabilities to extend and cascade layouts.
185

  
186

  
187
=head1 IMPLEMENTING LAYOUT CALLBACKS
188

  
189
There are eight callbacks (C<pre_content>, C<post_content>, C<start_content>,
190
C<end_content>, C<stylesheets>, C<stylesheets_inline>, C<javscripts>,
191
C<javascripts_inline>) which are documented in L<SL::Layout::Dispatcher>. If
192
you are writing a new simple layout, you can just override some of them like
193
this:
194

  
195
  package SL::Layout::MyEvilLayout;
196

  
197
  sub pre_content {
198
    '<h1>This is MY page now</h1>'
199
  }
200

  
201
  sub post_content {
202
    '<p align="right"><small><em>Brought to you by my own layout class</em></small></p>'
203
  }
204

  
205

  
206
To preserve the sanitizing effects of C<stylesheets> and C<javascripts> you should instead do the following:
207

  
208
  sub stylesheets {
209
    $_[0]->add_stylesheets(qw(mystyle1.css mystyle2.css);
210
    $_[0]->SUPER::stylesheets;
211
  }
212

  
213
If you want to add something to a different layout, you should write a sub
214
layout and add it to the other layouts.
215

  
216

  
217
=head1 SUB LAYOUTS
218

  
219
Layouts can be aggregated, so that common elements can be used in different
220
layouts. Currently this is used for the L<None|SL::Layout::None> sub layout,
221
which contains a lot of the stylesheets and javascripts necessary. Another
222
example is the L<Top|SL::Layout::Top> layout, which is used to generate a
223
common top bar for all menu types.
224

  
225
To add a sub layout to your layout just overwrite the sub_layout method:
226

  
227
  package SL::Layout::MyFinalLayout;
228

  
229
  sub init_sub_layout {
230
    [
231
      SL::Layout::None->new,
232
      SL::Layout::MyEvilLayout->new,
233
    ]
234
  }
235

  
236
You can also add a sublayout at runtime:
237

  
238
  $layout->add_sub_layout(SL::Layout::SideBar->new);
239

  
240
The standard implementation for the callbacks will see to it that the contents
241
of all sub layouts will get rendered.
242

  
243

  
244
=head1 COMBINING SUB LAYOUTS AND OWN BEHAVIOUR
245

  
246
This is still somewhat rough, and improvements are welcome.
247

  
248
For the C<*_content> callbacks this works if you just remember to dispatch to the base method:
249

  
250
  sub post_content {
251
    return $_[0]->render_status_bar .
252
    $_[0]->SUPER::post_content
253
  }
254

  
255
For the stylesheet and javascript callbacks things are hard, because of the
256
backwards compatibility, and the built-in sanity checks. The best way currently
257
is to just add your content and dispatch to the base method.
258

  
259
  sub stylesheets {
260
    $_[0]->add_stylesheets(qw(mystyle1.css mystyle2.css);
261
    $_[0]->SUPER::stylesheets;
262
  }
263

  
264
=head1 GORY DETAILS ABOUT JAVASCRIPT AND STYLESHEET OVERLOADING
265

  
266
The original code used to store one stylehsheet in C<< $form->{stylesheet} >> and
267
allowed/expected authors of potential C<bin/mozilla/> controllers to change
268
that into their own modified stylesheet.
269

  
270
This was at some point cleaned up into a method C<use stylesheet> which took a
271
string of space separated stylesheets and processed them into the response.
272

  
273
A lot of controllers are still using this methods so the layout interface
274
supports it to change as few controller code as possible, while providing the
275
more intuitive C<add_stylesheets> method.
276

  
277
At the same time the following things need to be possible:
278

  
279
=over 4
280

  
281
=item 1.
282

  
283
Runtime additions.
284

  
285
  $layout->add_stylesheets(...)
286

  
287
Since add_stylesheets adds to C<< $self->{stylesheets} >> there must be a way to read
288
from it. Currently this is the deprecated C<use_stylesheet>.
289

  
290
=item 2.
291

  
292
Overriding Callbacks
293

  
294
A leaf layout should be able to override a callback to return a list.
295

  
296
=item 3.
297

  
298
Sanitizing
299

  
300
C<stylesheets> needs to retain it's sanitizing behaviour.
301

  
302
=item 4.
303

  
304
Aggregation
305

  
306
The standard implementation should be able to collect from sub layouts.
307

  
308
=item 5.
309

  
310
Preserving of Inclusion Order
311

  
312
Since there is currently no standard way of mixing own content and including
313
sub layouts, this has to be done manually. Certain things like jquery get added
314
in L<SL::Layout::None> so that they get rendered first.
315

  
316
=back
317

  
318
The current implementation provides no good candidate for overriding in sub
319
classes, which should be changed. The other points work pretty well.
320

  
321
=head1 BUGS
322

  
323
None yet, if you don't count the horrible stylesheet/javascript interface.
324

  
325
=head1 AUTHOR
326

  
327
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
328

  
329
=cut
330

  
SL/Layout/Dispatcher.pm
20 20
}
21 21

  
22 22
1;
23

  
24
__END__
25

  
26
=encoding utf-8
27

  
28
=head1 NAME
29

  
30
SL::Layout::Dispatcher - provides layouts by name.
31

  
32
=head1 SYNOPSIS
33

  
34
  use SL::Layout::Dispatcher;
35
  $::request->layout(SL::Layout::Dispatcher->new(style => 'login'));
36

  
37
  # same as
38

  
39
  use SL::Layout::Login;
40
  $::request->layout(SL::Layout::Login->new);
41

  
42
=head1 INTRODUCTION
43

  
44
A layout in kivitendo is anything that should be included into a text/html
45
response without having each controller do it manually. This includes:
46

  
47
=over 4
48

  
49
=item *
50

  
51
menus
52

  
53
=item *
54

  
55
status bars
56

  
57
=item
58

  
59
div containers for error handling and ajax responses
60

  
61
=item *
62

  
63
javascript and css includes
64

  
65
=item *
66

  
67
html header and body
68

  
69
=back
70

  
71
It does not include:
72

  
73
=over 4
74

  
75
=item *
76

  
77
http headers
78

  
79
=item *
80

  
81
anthing that is not a text/html response
82

  
83
=back
84

  
85
All of these tasks are handled by a layout object, which is stored in the
86
global L<$::request|SL::Request> object. An appropriate layout object will be
87
chosen during the login/session_restore process.
88

  
89

  
90
=head1 INTERFACE
91

  
92
Every layout must be instantiated from L<SL::Layout::Base> and must implement
93
the following eight callbacks:
94

  
95
=over 4
96

  
97
=item C<pre_content>
98

  
99
Content that must, for whatever reason, appear before the main content.
100

  
101
=item C<start_content>
102

  
103
An introcutory clause for the main content. Usually something like C<< <div
104
class='content'> >>.
105

  
106
=item C<end_content>
107

  
108
The corresponding end of L</start_content> like C<< </div> >>
109

  
110
=item C<post_content>
111

  
112
Any extra content that should appear after the main content in the response
113
source. Note that it is preferred to put extra content after the main content,
114
so that it gets rendered faster.
115

  
116
=item C<stylesheets>
117

  
118
A list of stylesheets that should be included as a full relative web path. Will
119
be rendered into the html header.
120

  
121
=item C<stylesheets_inline>
122

  
123
A list of stylesheet snippets that need to be included in the response. Will be
124
added to the html header.
125

  
126
=item C<javascripts>
127

  
128
A list of javascripts that should be included as a full relative web path.
129

  
130
Note:
131
There is no guarantee where these will end up in the content. Currently they
132
will be rendered into the header, but are likely to be moved into the footer in
133
the future.
134

  
135
=item C<javascripts_inline>
136

  
137
A list of javascript snippets that need to be included in the response.
138

  
139
Note:
140
These will end up in the footer, so make sure they don't contain
141
initializations to static javascript includes that may be included earlier.
142

  
143
=back
144

  
145
=head1 RUNTIME INTERFACE
146

  
147
Each layout object can add stylesheets and javascripts at runtime, as long as
148
its before the actual rendering has begun. This can be used to add special
149
javascripts only your controller needs.
150

  
151
=over 4
152

  
153
=item C<add_stylesheets>
154

  
155
Adds the list of arguments to the list of used stylesheets.
156

  
157
These will first be searched in the theme folder for the current user, and only
158
after that be searched from the common C<css/> folder.
159

  
160
Duplicated files will be only included once.
161

  
162
Non-existing files will be pruned from the list.
163

  
164
=item C<use_stylesheet>
165

  
166
Backwards compatible alias for C<add_stylesheets>. Deprecated.
167

  
168
=item C<add_javascripts>
169

  
170
Adds the list of arguments to the list of used javascripts.
171

  
172
Duplicated files will be only included once.
173

  
174
Non-existing files will be pruned from the list.
175

  
176
=item C<use_javascript>
177

  
178
Backwards compatible alias for C<add_javascripts>. Deprecated.
179

  
180
=item C<add_javascripts_inline>
181

  
182
Add a snippet of javascript.
183

  
184
=item C<add_stylesheets_inline>
185

  
186
Add a snippet of css.
187

  
188
=item C<focus>
189

  
190
If set with a selector, the layout will generate javascript to set the page
191
focus to that selector on document.ready.
192

  
193
=back
194

  
195
=head1 BUGS
196

  
197
None yet :)
198

  
199
=head1 TODO
200

  
201
non existing css or js includes should generate a log entry.
202

  
203
=head1 AUTHOR
204

  
205
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
206

  
207
=cut
SL/Request.pm
522 522

  
523 523
Returns the requested content type (either C<html>, C<js> or C<json>).
524 524

  
525
=item C<layout>
526

  
527
Set and retrieve the layout object for the current request. Must be an instance
528
of L<SL::Layout::Base>. Defaults to an isntance of L<SL::Layout::None>.
529

  
530
For more information about layouts, see L<SL::Layout::Dispatcher>.
531

  
525 532
=back
526 533

  
527 534
=head1 SPECIAL FUNCTIONS

Auch abrufbar als: Unified diff