WritingPages

As you know, Catalyst is the Elegant MVC Web Application Framework. MVC is what makes it elegant - it separates the database code (Model) from the URI handling code (Controller) from the HTML-formatting code (View).

Controller

Each URI maps to a controller method.

Create a new controller called 'Foo', like so:

retout@bermuda:~/var/www/UWCS$ script/uwcs_create.pl controller Foo
 exists "/home/retout/var/www/UWCS/script/../lib/UWCS/Controller"
 exists "/home/retout/var/www/UWCS/script/../t"
created "/home/retout/var/www/UWCS/script/../lib/UWCS/Controller/Foo.pm"
created "/home/retout/var/www/UWCS/script/../t/controller_Foo.t"

This will generate some default files for you. Edit lib/UWCS/Controller/Foo.pm, and create a new empty method called 'bar'.

sub bar : Local {
        my ( $self, $c ) = @_;

        # Model handling code would go here.
}

Note the 'Local' attribute after 'bar'. Actions are explained in detail in Catalyst::Manual::Intro.

You now have a Foo.pm controller, with a single method. It will handle URIs of the form http://path-to-test-server/foo/bar, and anything below that.

View

The UWCS website is using the Template Toolkit to implement the default View.

Templates are kept under two locations:

  • root/lib - general site templates (headers, footers)
  • root/src - page-specific templates

Each controller method will map to a default template file, as explained in the documentation for Catalyst::View::TT, under 'TEMPLATE_EXTENSION'.

In the case of our example, we want to create a template at root/src/foo/bar.tt:

[% page = {
        title = 'Foo Bar'
   }
-%]

<p>This is a test page to experiment with writing pages for the UWCS website.</p>

Testing

Now start the server, by running script/uwcs_server.pl. If it was already running, you would have to restart it. Browse to http://localhost:3000/foo/bar and check that it works.