Catalyst-Manual

 view release on metacpan or  search on metacpan

lib/Catalyst/Manual/Tutorial/02_CatalystBasics.pod  view on Meta::CPAN

also probably upgrade before continuing the tutorial.)

Some MVC frameworks handle dispatching in a central place. Catalyst, by
policy, prefers to handle URL dispatching with attributes on controller
methods. There is a lot of flexibility in specifying which URLs to
match.  This particular method will match all URLs, because it doesn't
specify the path (nothing comes after "Path"), but will only accept a
URL without any args because of the "C<:Args(0)>".

The default is to map URLs to controller names, and because of the way
that Perl handles namespaces through package names, it is simple to
create hierarchical structures in Catalyst. This means that you can
create controllers with deeply nested actions in a clean and logical
way. For example, the URL C<http://hello.com/admin/articles/create> maps
to the package C<Hello::Controller::Admin::Articles>, and the C<create>
method.

While you leave the C<script/hello_server.pl -r> command running the
development server in one window (don't forget the "-r" at the end!),
open another window and add the following subroutine to your
F<lib/Hello/Controller/Root.pm> file:

    sub hello :Global {
        my ( $self, $c ) = @_;

        $c->response->body("Hello, World!");
    }

B<TIP>: See Appendix 1 for tips on removing the leading spaces when
cutting and pasting example code from POD-based documents.

Notice in the window running the Development Server that you should
get output similar to the following:

    Saw changes to the following files:
     - /home/catalyst/Hello/lib/Hello/Controller/Root.pm (modify)

    Attempting to restart the server
    ...
    [debug] Loaded Private actions:
    .----------------------+--------------------------------------+--------------.
    | Private              | Class                                | Method       |
    +----------------------+--------------------------------------+--------------+
    | /default             | Hello::Controller::Root              | default      |
    | /end                 | Hello::Controller::Root              | end          |
    | /index               | Hello::Controller::Root              | index        |
    | /hello               | Hello::Controller::Root              | hello        |
    '----------------------+--------------------------------------+--------------'
    ...

The development server noticed the change in C<Hello::Controller::Root>
and automatically restarted itself.

Go to L<http://localhost:3000/hello> to see "Hello, World!".   Also
notice that the newly defined 'hello' action is listed under "Loaded
Private actions" in the development server debug output.


=head2 Hello, World! Using a View and a Template

In the Catalyst world a "View" itself is not a page of XHTML or a
template designed to present a page to a browser. Rather, it is the
module that determines the I<type> of view -- HTML, PDF, XML, etc. For
the thing that generates the I<content> of that view (such as a
Template Toolkit template file), the actual templates go under the
"root" directory.

To create a TT view, run:

    $ script/hello_create.pl view HTML TT

This creates the F<lib/Hello/View/HTML.pm> module, which is a subclass
of L<Catalyst::View::TT>.

=over 4

=item *

The "view" keyword tells the create script that you are creating a view.

=item *

The first argument "HTML" tells the script to name the View module "HTML.pm",
which is a commonly used name for TT views.  You can name it anything you want,
such as "MyView.pm". If you have more than one view, be sure to set the
default_view in Hello.pm (See L<Catalyst::View::TT> for more
details on setting this).

=item *

The final "TT" tells Catalyst the I<type> of the view, with "TT"
indicating that you want to use a Template Toolkit view.

=back

If you look at F<lib/Hello/View/HTML.pm> you will find that it only
contains a config statement to set the TT extension to ".tt".

Now that the HTML.pm "View" exists, Catalyst will autodiscover it and be
able to use it to display the view templates using the "process" method
that it inherits from the L<Catalyst::View::TT> class.

Template Toolkit is a very full-featured template facility, with
excellent documentation at L<http://template-toolkit.org/>, but since
this is not a TT tutorial, we'll stick to only basic TT usage here (and
explore some of the more common TT features in later chapters of the
tutorial).

Create a F<root/hello.tt> template file (put it in the C<root> under the
C<Hello> directory that is the base of your application). Here is a
simple sample:

    <p>
        This is a TT view template, called '[% template.name %]'.
    </p>

[% and %] are markers for the TT parts of the template. Inside you can
access Perl variables and classes, and use TT directives. In this case,
we're using a special TT variable that defines the name of the template
file (F<hello.tt>).  The rest of the template is normal HTML.



( run in 1.511 second using v1.01-cache-2.11-cpan-677af5a14d3 )