Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / 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 HTML 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 { # Or use Catalyst::Action::RenderView
68         my ( $self, $c ) = @_;
69         $c->forward( $c->view('HTML') );
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     render_die   => 1,
171 });
172
173 =head1 NAME
174
175 [% class %] - Catalyst TTSite View
176
177 =head1 SYNOPSIS
178
179 See L<[% app %]>
180
181 =head1 DESCRIPTION
182
183 Catalyst TTSite View.
184
185 =head1 AUTHOR
186
187 [% author %]
188
189 =head1 LICENSE
190
191 This library is free software. You can redistribute it and/or modify
192 it under the same terms as Perl itself.
193
194 =cut
195
196 1;
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
206    # any other page, by virtue of it being defined as a PRE_PROCESS
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
221    # load up any other configuration items
222    PROCESS config/col
223          + config/url;
224
225    # set defaults for variables, etc.
226    DEFAULT
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    };
252 -%]
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)');
266      debug("Passing page through as text: $template.name");
267      content;
268    ELSE;
269      debug("Applying HTML page layout wrappers to $template.name\n");
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>
314   This is the welcome page.  Why not try the equally-exciting
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>
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
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 %]
349 html {
350     height: 100%;
351 }
352
353 body {
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
381 h1.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 }