| 1 | package Catalyst::Helper::View::TTSite; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use File::Spec; | ||
| 5 | |||
| 6 | our $VERSION = '0.46'; | ||
| 7 | $VERSION =~ tr/_//d; | ||
| 8 | |||
| 9 | sub mk_compclass { | ||
| 10 | my ( $self, $helper, @args ) = @_; | ||
| 11 | my $file = $helper->{file}; | ||
| 12 | $helper->render_file( 'compclass', $file ); | ||
| 13 | $self->mk_templates( $helper, @args ); | ||
| 14 | } | ||
| 15 | |||
| 16 | sub mk_templates { | ||
| 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' ); | ||
| 21 | |||
| 22 | $helper->mk_dir($ldir); | ||
| 23 | $helper->mk_dir($sdir); | ||
| 24 | |||
| 25 | my $dir = File::Spec->catfile( $ldir, 'config' ); | ||
| 26 | $helper->mk_dir($dir); | ||
| 27 | |||
| 28 | foreach my $file (qw( main col url )) { | ||
| 29 | $helper->render_file( "config_$file", | ||
| 30 | File::Spec->catfile( $dir, $file ) ); | ||
| 31 | } | ||
| 32 | |||
| 33 | $dir = File::Spec->catfile( $ldir, 'site' ); | ||
| 34 | $helper->mk_dir($dir); | ||
| 35 | |||
| 36 | foreach my $file (qw( wrapper layout html header footer )) { | ||
| 37 | $helper->render_file( "site_$file", | ||
| 38 | File::Spec->catfile( $dir, $file ) ); | ||
| 39 | } | ||
| 40 | |||
| 41 | foreach my $file (qw( welcome.tt2 message.tt2 error.tt2 ttsite.css )) { | ||
| 42 | $helper->render_file( $file, File::Spec->catfile( $sdir, $file ) ); | ||
| 43 | } | ||
| 44 | |||
| 45 | } | ||
| 46 | |||
| 47 | =head1 NAME | ||
| 48 | |||
| 49 | Catalyst::Helper::View::TTSite - Helper for TT view which builds a skeleton web site | ||
| 50 | |||
| 51 | =head1 SYNOPSIS | ||
| 52 | |||
| 53 | # use the helper to create the view module and templates | ||
| 54 | |||
| 55 | $ script/myapp_create.pl view HTML TTSite | ||
| 56 | |||
| 57 | # add something like the following to your main application module | ||
| 58 | |||
| 59 | sub message : Global { | ||
| 60 | my ( $self, $c ) = @_; | ||
| 61 | $c->stash->{template} = 'message.tt2'; | ||
| 62 | $c->stash->{message} ||= $c->req->param('message') || 'No message'; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub default : Private { | ||
| 66 | my ( $self, $c ) = @_; | ||
| 67 | $c->stash->{template} = 'welcome.tt2'; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub end : Private { # Or use Catalyst::Action::RenderView | ||
| 71 | my ( $self, $c ) = @_; | ||
| 72 | $c->forward( $c->view('HTML') ); | ||
| 73 | } | ||
| 74 | |||
| 75 | =head1 DESCRIPTION | ||
| 76 | |||
| 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. | ||
| 80 | |||
| 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. | ||
| 86 | |||
| 87 | The view module that the helper creates is automatically configured | ||
| 88 | to locate these templates. | ||
| 89 | |||
| 90 | =head2 Default Rendering | ||
| 91 | |||
| 92 | To render a template the following process is applied: | ||
| 93 | |||
| 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. | ||
| 101 | |||
| 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>. | ||
| 106 | |||
| 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: | ||
| 109 | |||
| 110 | =over 4 | ||
| 111 | |||
| 112 | =item * F<root/src/your_template.tt2> | ||
| 113 | |||
| 114 | =item * F<root/lib/site/footer> | ||
| 115 | |||
| 116 | =item * F<root/lib/site/header> | ||
| 117 | |||
| 118 | =item * F<root/lib/site/layout> | ||
| 119 | |||
| 120 | =item * F<root/lib/site/html> | ||
| 121 | |||
| 122 | =back | ||
| 123 | |||
| 124 | Finally, the rendered content is returned to the browser. | ||
| 125 | |||
| 126 | =head1 METHODS | ||
| 127 | |||
| 128 | =head2 mk_compclass | ||
| 129 | |||
| 130 | Generates the component class. | ||
| 131 | |||
| 132 | =head2 mk_templates | ||
| 133 | |||
| 134 | Generates the templates. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | =head1 SEE ALSO | ||
| 139 | |||
| 140 | L<Catalyst>, L<Catalyst::View::TT>, L<Catalyst::Helper>, | ||
| 141 | L<Catalyst::Helper::View::TT> | ||
| 142 | |||
| 143 | =head1 AUTHOR | ||
| 144 | |||
| 145 | Andy Wardley <abw@cpan.org> | ||
| 146 | |||
| 147 | =head1 LICENSE | ||
| 148 | |||
| 149 | This library is free software. You can redistribute it and/or modify | ||
| 150 | it under the same terms as perl itself. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | 1; | ||
| 155 | |||
| 156 | __DATA__ |