Update helpers to be slightly more best practice
[catagits/Catalyst-View-TT.git] / lib / Catalyst / Helper / View / TTSite.pm
CommitLineData
8cd017a8 1package Catalyst::Helper::View::TTSite;
2
3use strict;
4use File::Spec;
5
6sub mk_compclass {
7 my ( $self, $helper, @args ) = @_;
8 my $file = $helper->{file};
9 $helper->render_file( 'compclass', $file );
10 $self->mk_templates( $helper, @args );
11}
12
13sub mk_templates {
14 my ( $self, $helper ) = @_;
19ee577a 15 my $base = $helper->{base},;
16 my $ldir = File::Spec->catfile( $base, 'root', 'lib' );
17 my $sdir = File::Spec->catfile( $base, 'root', 'src' );
8cd017a8 18
19 $helper->mk_dir($ldir);
20 $helper->mk_dir($sdir);
21
22 my $dir = File::Spec->catfile( $ldir, 'config' );
23 $helper->mk_dir($dir);
24
25 foreach my $file (qw( main col url )) {
26 $helper->render_file( "config_$file",
27 File::Spec->catfile( $dir, $file ) );
28 }
29
30 $dir = File::Spec->catfile( $ldir, 'site' );
31 $helper->mk_dir($dir);
32
33 foreach my $file (qw( wrapper layout html header footer )) {
34 $helper->render_file( "site_$file",
35 File::Spec->catfile( $dir, $file ) );
36 }
37
38 foreach my $file (qw( welcome.tt2 message.tt2 error.tt2 ttsite.css )) {
39 $helper->render_file( $file, File::Spec->catfile( $sdir, $file ) );
40 }
41
42}
43
44=head1 NAME
45
46Catalyst::Helper::View::TTSite - Helper for TT view which builds a skeleton web site
47
48=head1 SYNOPSIS
49
50# use the helper to create the view module and templates
51
90798082 52 $ script/myapp_create.pl view HTML TTSite
8cd017a8 53
54# add something like the following to your main application module
55
56 sub message : Global {
94b3529a 57 my ( $self, $c ) = @_;
58 $c->stash->{template} = 'message.tt2';
19ee577a 59 $c->stash->{message} ||= $c->req->param('message') || 'No message';
8cd017a8 60 }
8544227a 61
8cd017a8 62 sub default : Private {
94b3529a 63 my ( $self, $c ) = @_;
64 $c->stash->{template} = 'welcome.tt2';
8cd017a8 65 }
8544227a 66
90798082 67 sub end : Private { # Or use Catalyst::Action::RenderView
94b3529a 68 my ( $self, $c ) = @_;
90798082 69 $c->forward( $c->view('HTML') );
8cd017a8 70 }
71
72=head1 DESCRIPTION
73
74This helper module creates a TT View module. It goes further than
75Catalyst::Helper::View::TT in that it additionally creates a simple
76set of templates to get you started with your web site presentation.
77
2d93834f 78It creates the templates in F<root/> directory underneath your
8cd017a8 79main project directory. In here two further subdirectories are
2d93834f 80created: F<root/src> which contains the main page templates, and F<root/lib>
81containing a library of other template components (header, footer,
8cd017a8 82etc.) that the page templates use.
83
84The view module that the helper creates is automatically configured
85to locate these templates.
86
8544227a 87=head2 Default Rendering
8cd017a8 88
2d93834f 89To render a template the following process is applied:
90
91The configuration template F<root/lib/config/main> is rendered. This is
92controlled by the C<PRE_PROCESS> configuration variable set in the controller
93generated by Catalyst::Helper::View::TTsite. Additionally, templates referenced by
94the C<PROCESS> directive will then be rendered. By default the following additional
95templates are set: F<root/lib/config/col>,
96which defines color names and RGB their RGB values and F</root/lib/config/url>,
97which defines site wide variables available to templates.
98
99Next, the template defined by the C<WRAPPER> config variable is called. The default
100wrapper template is located in F<root/lib/site/wrapper>. The wrapper template
101passes files with C<.css/.js/.txt> extensions through as text OR processes
102the templates defined after the C<WRAPPER> directive: C<site/html> and C<site/layout>.
103
104Based on the default value of the C<WRAPPER> directive in F<root/lib/site/wrapper>,
105the following templates are processed in order:
106
107=over 4
108
109=item * F<root/src/your_template.tt2>
110
111=item * F<root/lib/site/footer>
112
113=item * F<root/lib/site/header>
114
115=item * F<root/lib/site/layout>
116
117=item * F<root/lib/site/html>
118
119=back
120
121Finally, the rendered content is returned to the bowser.
122
123=head1 METHODS
124
125=head2 mk_compclass
8cd017a8 126
127Generates the component class.
128
2d93834f 129=head2 mk_templates
8cd017a8 130
131Generates the templates.
132
133=cut
134
135=head1 SEE ALSO
136
137L<Catalyst>, L<Catalyst::View::TT>, L<Catalyst::Helper>,
138L<Catalyst::Helper::View::TT>
139
140=head1 AUTHOR
141
142Andy Wardley <abw@cpan.org>
143
144=head1 LICENSE
145
8544227a 146This library is free software. You can redistribute it and/or modify
8cd017a8 147it under the same terms as perl itself.
148
149=cut
150
1511;
152
153__DATA__
154
155__compclass__
156package [% class %];
157
158use strict;
159use base 'Catalyst::View::TT';
8cd017a8 160
f4841eb0 161__PACKAGE__->config({
795f7999 162 INCLUDE_PATH => [
4d97e86f 163 [% app %]->path_to( 'root', 'src' ),
164 [% app %]->path_to( 'root', 'lib' )
795f7999 165 ],
f4841eb0 166 PRE_PROCESS => 'config/main',
167 WRAPPER => 'site/wrapper',
94b3529a 168 ERROR => 'error.tt2',
90798082 169 TIMER => 0,
170 render_die => 1,
f4841eb0 171});
8cd017a8 172
173=head1 NAME
174
795f7999 175[% class %] - Catalyst TTSite View
8cd017a8 176
177=head1 SYNOPSIS
178
179See L<[% app %]>
180
181=head1 DESCRIPTION
182
795f7999 183Catalyst TTSite View.
8cd017a8 184
185=head1 AUTHOR
186
187[% author %]
188
189=head1 LICENSE
190
8544227a 191This library is free software. You can redistribute it and/or modify
795f7999 192it under the same terms as Perl itself.
8cd017a8 193
194=cut
195
1961;
197
198__config_main__
199[% USE Date;
200 year = Date.format(Date.now, '%Y');
201-%]
202[% TAGS star -%]
203[% # config/main
204 #
205 # This is the main configuration template which is processed before
8544227a 206 # any other page, by virtue of it being defined as a PRE_PROCESS
8cd017a8 207 # template. This is the place to define any extra template variables,
208 # macros, load plugins, and perform any other template setup.
209
210 IF Catalyst.debug;
211 # define a debug() macro directed to Catalyst's log
212 MACRO debug(message) CALL Catalyst.log.debug(message);
213 END;
214
215 # define a data structure to hold sitewide data
216 site = {
217 title => 'Catalyst::View::TTSite Example Page',
218 copyright => '[* year *] Your Name Here',
219 };
220
8544227a 221 # load up any other configuration items
8cd017a8 222 PROCESS config/col
223 + config/url;
224
225 # set defaults for variables, etc.
8544227a 226 DEFAULT
8cd017a8 227 message = 'There is no message';
228
229-%]
230__config_col__
231[% TAGS star -%]
232[% site.rgb = {
233 black = '#000000'
234 white = '#ffffff'
235 grey1 = '#46494c'
236 grey2 = '#c6c9cc'
237 grey3 = '#e3e6ea'
238 red = '#CC4444'
239 green = '#66AA66'
240 blue = '#89b8df'
241 orange = '#f08900'
242 };
243
244 site.col = {
245 page = site.rgb.white
246 text = site.rgb.grey1
247 head = site.rgb.grey3
248 line = site.rgb.orange
249 message = site.rgb.green
250 error = site.rgb.red
251 };
c67cc83b 252-%]
8cd017a8 253__config_url__
254[% TAGS star -%]
255[% base = Catalyst.req.base;
256
257 site.url = {
258 base = base
259 home = "${base}welcome"
260 message = "${base}message"
261 }
262-%]
263__site_wrapper__
264[% TAGS star -%]
265[% IF template.name.match('\.(css|js|txt)');
94b3529a 266 debug("Passing page through as text: $template.name");
8cd017a8 267 content;
268 ELSE;
94b3529a 269 debug("Applying HTML page layout wrappers to $template.name\n");
8cd017a8 270 content WRAPPER site/html + site/layout;
271 END;
272-%]
273__site_html__
274[% TAGS star -%]
275<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
276<html>
277 <head>
278 <title>[% template.title or site.title %]</title>
279 <style type="text/css">
280[% PROCESS ttsite.css %]
281 </style>
282 </head>
283 <body>
284[% content %]
285 </body>
286</html>
287__site_layout__
288[% TAGS star -%]
289<div id="header">[% PROCESS site/header %]</div>
290
291<div id="content">
292[% content %]
293</div>
294
295<div id="footer">[% PROCESS site/footer %]</div>
296__site_header__
297[% TAGS star -%]
298<!-- BEGIN site/header -->
299<h1 class="title">[% template.title or site.title %]</h1>
300<!-- END site/header -->
301__site_footer__
302[% TAGS star -%]
303<!-- BEGIN site/footer -->
304<div id="copyright">&copy; [% site.copyright %]</div>
305<!-- END site/footer -->
306__welcome.tt2__
307[% TAGS star -%]
308[% META title = 'Catalyst/TT View!' %]
309<p>
310 Yay! You're looking at a page generated by the Catalyst::View::TT
311 plugin module.
312</p>
313<p>
8544227a 314 This is the welcome page. Why not try the equally-exciting
8cd017a8 315 <a href="[% site.url.message %]">Message Page</a>?
316</p>
317__message.tt2__
318[% TAGS star -%]
319[% META title = 'Catalyst/TT View!' %]
320<p>
321 Yay! You're looking at a page generated by the Catalyst::View::TT
322 plugin module.
323</p>
324<p>
325 We have a message for you: <span class="message">[% message %]</span>.
326</p>
327<p>
328 Why not try updating the message? Go on, it's really exciting, honest!
329</p>
330<form action="[% site.url.message %]"
331 method="POST" enctype="application/x-www-form-urlencoded">
332 <input type="text" name="message" value="[% message %]" />
333 <input type="submit" name="submit" value=" Update Message "/>
334</form>
335__error.tt2__
336[% TAGS star -%]
337[% META title = 'Catalyst/TT Error' %]
338<p>
8544227a 339 An error has occurred. We're terribly sorry about that, but it's
340 one of those things that happens from time to time. Let's just
8cd017a8 341 hope the developers test everything properly before release...
342</p>
343<p>
344 Here's the error message, on the off-chance that it means something
345 to you: <span class="error">[% error %]</span>
346</p>
347__ttsite.css__
348[% TAGS star %]
349html {
350 height: 100%;
351}
352
8544227a 353body {
8cd017a8 354 background-color: [% site.col.page %];
355 color: [% site.col.text %];
356 margin: 0px;
357 padding: 0px;
358 height: 100%;
359}
360
361#header {
362 background-color: [% site.col.head %];
363 border-bottom: 1px solid [% site.col.line %];
364}
365
366#footer {
367 background-color: [% site.col.head %];
368 text-align: center;
369 border-top: 1px solid [% site.col.line %];
370 position: absolute;
371 bottom: 0;
372 left: 0px;
373 width: 100%;
374 padding: 4px;
375}
376
377#content {
378 padding: 10px;
379}
380
381h1.title {
382 padding: 4px;
383 margin: 0px;
384}
385
386.message {
387 color: [% site.col.message %];
388}
389
390.error {
391 color: [% site.col.error %];
392}