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