Catalyst-Manual

 view release on metacpan or  search on metacpan

lib/Catalyst/Manual/Tutorial/03_MoreCatalystBasics.pod  view on Meta::CPAN


Point your browser to L<http://localhost:3000> and you should still get
the Catalyst welcome page.

Next, to view the book list, change the URL in your browser to
L<http://localhost:3000/books/list>. You should get a list of the five
books loaded by the F<myapp01.sql> script above without any formatting.
The rating for each book should appear on each row, but the "Author(s)"
column will still be blank (we will fill that in later).

Also notice in the output of the F<script/myapp_server.pl> that
L<DBIx::Class> used the following SQL to retrieve the data:

    SELECT me.id, me.title, me.rating FROM book me

because we enabled DBIC_TRACE.

You now have the beginnings of a simple but workable web application.
Continue on to future sections and we will develop the application more
fully.


=head1 CREATE A WRAPPER FOR THE VIEW

When using TT, you can (and should) create a wrapper that will literally
wrap content around each of your templates.  This is certainly useful as
you have one main source for changing things that will appear across
your entire site/application instead of having to edit many individual
files.


=head2 Configure HTML.pm For The Wrapper

In order to create a wrapper, you must first edit your TT view and tell
it where to find your wrapper file.

Edit your TT view in F<lib/MyApp/View/HTML.pm> and change it to match
the following:

    __PACKAGE__->config(
        # Change default TT extension
        TEMPLATE_EXTENSION => '.tt2',
        # Set the location for TT files
        INCLUDE_PATH => [
                MyApp->path_to( 'root', 'src' ),
            ],
        # Set to 1 for detailed timer stats in your HTML as comments
        TIMER              => 0,
        # This is your wrapper template located in the 'root/src'
        WRAPPER => 'wrapper.tt2',
    );


=head2 Create the Wrapper Template File and Stylesheet

Next you need to set up your wrapper template.  Basically, you'll want
to take the overall layout of your site and put it into this file.  For
the tutorial, open F<root/src/wrapper.tt2> and input the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" [%#
        %]"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>[% template.title or "My Catalyst App!" %]</title>
    <link rel="stylesheet" href="[% c.uri_for('/static/css/main.css') %]" />
    </head>

    <body>
    <div id="outer">
    <div id="header">
        [%# Your logo could go here -%]
        <img src="[% c.uri_for('/static/images/btn_88x31_powered.png') %]" />
        [%# Insert the page title -%]
        <h1>[% template.title or site.title %]</h1>
    </div>

    <div id="bodyblock">
    <div id="menu">
        Navigation:
        <ul>
            <li><a href="[% c.uri_for('/books/list') %]">Home</a></li>
            <li><a href="[% c.uri_for('/')
                %]" title="Catalyst Welcome Page">Welcome</a></li>
        </ul>
    </div><!-- end menu -->

    <div id="content">
        [%# Status and error messages %]
        <span class="message">[% status_msg %]</span>
        <span class="error">[% error_msg %]</span>
        [%# This is where TT will stick all of your template's contents. -%]
        [% content %]
    </div><!-- end content -->
    </div><!-- end bodyblock -->

    <div id="footer">Copyright (c) your name goes here</div>
    </div><!-- end outer -->

    </body>
    </html>

Notice the status and error message sections in the code above:

    <span class="status">[% status_msg %]</span>
    <span class="error">[% error_msg %]</span>

If we set either message in the Catalyst stash (e.g.,
C<< $c->stash->{status_msg} = 'Request was successful!' >>) it will
be displayed whenever any view used by that request is rendered.  The
C<message> and C<error> CSS styles can be customized to suit your needs
in the F<root/static/css/main.css> file we create below.

B<Notes:>

=over 4

=item *

The Catalyst stash only lasts for a single HTTP request.  If you need to
retain information across requests you can use



( run in 2.228 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )