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