1 package Catalyst::Helper::View::TTSite;
7 $VERSION = eval $VERSION;
10 my ( $self, $helper, @args ) = @_;
11 my $file = $helper->{file};
12 $helper->render_file( 'compclass', $file );
13 $self->mk_templates( $helper, @args );
17 my ( $self, $helper ) = @_;
18 my $base = $helper->{base},;
19 my $ldir = File::Spec->catfile( $base, 'root', 'lib' );
20 my $sdir = File::Spec->catfile( $base, 'root', 'src' );
22 $helper->mk_dir($ldir);
23 $helper->mk_dir($sdir);
25 my $dir = File::Spec->catfile( $ldir, 'config' );
26 $helper->mk_dir($dir);
28 foreach my $file (qw( main col url )) {
29 $helper->render_file( "config_$file",
30 File::Spec->catfile( $dir, $file ) );
33 $dir = File::Spec->catfile( $ldir, 'site' );
34 $helper->mk_dir($dir);
36 foreach my $file (qw( wrapper layout html header footer )) {
37 $helper->render_file( "site_$file",
38 File::Spec->catfile( $dir, $file ) );
41 foreach my $file (qw( welcome.tt2 message.tt2 error.tt2 ttsite.css )) {
42 $helper->render_file( $file, File::Spec->catfile( $sdir, $file ) );
49 Catalyst::Helper::View::TTSite - Helper for TT view which builds a skeleton web site
53 # use the helper to create the view module and templates
55 $ script/myapp_create.pl view HTML TTSite
57 # add something like the following to your main application module
59 sub message : Global {
60 my ( $self, $c ) = @_;
61 $c->stash->{template} = 'message.tt2';
62 $c->stash->{message} ||= $c->req->param('message') || 'No message';
65 sub default : Private {
66 my ( $self, $c ) = @_;
67 $c->stash->{template} = 'welcome.tt2';
70 sub end : Private { # Or use Catalyst::Action::RenderView
71 my ( $self, $c ) = @_;
72 $c->forward( $c->view('HTML') );
77 This helper module creates a TT View module. It goes further than
78 Catalyst::Helper::View::TT in that it additionally creates a simple
79 set of templates to get you started with your web site presentation.
81 It creates the templates in F<root/> directory underneath your
82 main project directory. In here two further subdirectories are
83 created: F<root/src> which contains the main page templates, and F<root/lib>
84 containing a library of other template components (header, footer,
85 etc.) that the page templates use.
87 The view module that the helper creates is automatically configured
88 to locate these templates.
90 =head2 Default Rendering
92 To render a template the following process is applied:
94 The configuration template F<root/lib/config/main> is rendered. This is
95 controlled by the C<PRE_PROCESS> configuration variable set in the controller
96 generated by Catalyst::Helper::View::TTsite. Additionally, templates referenced by
97 the C<PROCESS> directive will then be rendered. By default the following additional
98 templates are set: F<root/lib/config/col>,
99 which defines color names and RGB their RGB values and F</root/lib/config/url>,
100 which defines site wide variables available to templates.
102 Next, the template defined by the C<WRAPPER> config variable is called. The default
103 wrapper template is located in F<root/lib/site/wrapper>. The wrapper template
104 passes files with C<.css/.js/.txt> extensions through as text OR processes
105 the templates defined after the C<WRAPPER> directive: C<site/html> and C<site/layout>.
107 Based on the default value of the C<WRAPPER> directive in F<root/lib/site/wrapper>,
108 the following templates are processed in order:
112 =item * F<root/src/your_template.tt2>
114 =item * F<root/lib/site/footer>
116 =item * F<root/lib/site/header>
118 =item * F<root/lib/site/layout>
120 =item * F<root/lib/site/html>
124 Finally, the rendered content is returned to the browser.
130 Generates the component class.
134 Generates the templates.
140 L<Catalyst>, L<Catalyst::View::TT>, L<Catalyst::Helper>,
141 L<Catalyst::Helper::View::TT>
145 Andy Wardley <abw@cpan.org>
149 This library is free software. You can redistribute it and/or modify
150 it under the same terms as perl itself.
162 use base 'Catalyst::View::TT';
164 __PACKAGE__->config({
166 [% app %]->path_to( 'root', 'src' ),
167 [% app %]->path_to( 'root', 'lib' )
169 PRE_PROCESS => 'config/main',
170 WRAPPER => 'site/wrapper',
171 ERROR => 'error.tt2',
178 [% class %] - Catalyst TTSite View
186 Catalyst TTSite View.
194 This library is free software. You can redistribute it and/or modify
195 it under the same terms as Perl itself.
203 year = Date.format(Date.now, '%Y');
208 # This is the main configuration template which is processed before
209 # any other page, by virtue of it being defined as a PRE_PROCESS
210 # template. This is the place to define any extra template variables,
211 # macros, load plugins, and perform any other template setup.
214 # define a debug() macro directed to Catalyst's log
215 MACRO debug(message) CALL Catalyst.log.debug(message);
218 # define a data structure to hold sitewide data
220 title => 'Catalyst::View::TTSite Example Page',
221 copyright => '[* year *] Your Name Here',
224 # load up any other configuration items
228 # set defaults for variables, etc.
230 message = 'There is no message';
248 page = site.rgb.white
249 text = site.rgb.grey1
250 head = site.rgb.grey3
251 line = site.rgb.orange
252 message = site.rgb.green
258 [% base = Catalyst.req.base;
262 home = "${base}welcome"
263 message = "${base}message"
268 [% IF template.name.match('\.(css|js|txt)');
269 debug("Passing page through as text: $template.name");
272 debug("Applying HTML page layout wrappers to $template.name\n");
273 content WRAPPER site/html + site/layout;
278 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
281 <title>[% template.title or site.title %]</title>
282 <style type="text/css">
283 [% PROCESS ttsite.css %]
292 <div id="header">[% PROCESS site/header %]</div>
298 <div id="footer">[% PROCESS site/footer %]</div>
301 <!-- BEGIN site/header -->
302 <h1 class="title">[% template.title or site.title %]</h1>
303 <!-- END site/header -->
306 <!-- BEGIN site/footer -->
307 <div id="copyright">© [% site.copyright %]</div>
308 <!-- END site/footer -->
311 [% META title = 'Catalyst/TT View!' %]
313 Yay! You're looking at a page generated by the Catalyst::View::TT
317 This is the welcome page. Why not try the equally-exciting
318 <a href="[% site.url.message %]">Message Page</a>?
322 [% META title = 'Catalyst/TT View!' %]
324 Yay! You're looking at a page generated by the Catalyst::View::TT
328 We have a message for you: <span class="message">[% message %]</span>.
331 Why not try updating the message? Go on, it's really exciting, honest!
333 <form action="[% site.url.message %]"
334 method="POST" enctype="application/x-www-form-urlencoded">
335 <input type="text" name="message" value="[% message %]" />
336 <input type="submit" name="submit" value=" Update Message "/>
340 [% META title = 'Catalyst/TT Error' %]
342 An error has occurred. We're terribly sorry about that, but it's
343 one of those things that happens from time to time. Let's just
344 hope the developers test everything properly before release...
347 Here's the error message, on the off-chance that it means something
348 to you: <span class="error">[% error %]</span>
357 background-color: [% site.col.page %];
358 color: [% site.col.text %];
365 background-color: [% site.col.head %];
366 border-bottom: 1px solid [% site.col.line %];
370 background-color: [% site.col.head %];
372 border-top: 1px solid [% site.col.line %];
390 color: [% site.col.message %];
394 color: [% site.col.error %];