Docendo discimus

$self->explain

  • Calendar

    July 2009
    M T W T F S S
    « Jun   Aug »
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Archives

  • Recent Posts

  • Bling

Catalyst is easy – fourth movement

Posted by brunorc on July 4, 2009

Sooner or later mixing HTML with actual application code will start to look very ugly. While it can be good for some rough drafts, real apps demand something more.

Catalyst treats all views in the same manner, so is really up to you to choose a model. Before using Catalyst I wrote some applications in Mason, so I was really happy being able to use this module as the View layer – also because of its outstanding documentation (and a book). Then I discovered TT (Template Toolkit), which have been used in Catalyst tutorial and I liked it – also because of its outstanding documentation!

I won’t get into details of installation of Mason, TT nor their respective Catalyst::View modules in this post. However, soon I’ll describe the installation procedure on Mac OS X, Ubuntu and (maybe even) Windows. On most systems you can do this using the cpan script. At this moment I assume you are able to install required modules. If not, just glance over this post and get back to it once you know how to install all the necessary stuff (maybe from one of my next posts).

I arbitrary decided to use TT. This is a good choice, not even but especially if you used to work with Mason before. That’s because Template Toolkit is what its name says. Sure, it lacks some capabilities of Mason, but in most cases they are not needed, since Catalyst already provide us with something at least similar. Using TT one is not tempted to stuff the logic into the view layer, acting rather the opposite way – eliminating the code from view, making the latter clean and simple.

But since it have to be easy, I won’t use the bare TT. Instead I will suggest you to use the marvellous TTSite module, which just builds the whole structure you need to have your web application powered by TT templates:

script/helloworld_create.pl view TTEasy TTSite

As you can see, it creates two directories in root, the root directory of documents served by our application. One of them – the src dir – contains all files that should be processed by TT and sent to the user. The other one – lib – is full of the TTSite magic, which moves all the configuration hassle almost out of the way.

DANGER, Will Robinson!

Chances are you won’t limit your Catalyst experience only to my blog. I wholeheartedly advise it to you; however, bear in mind that you will find tons of TT examples, addressing the Catalyst object as c, like [% c.uri_for('blah') %]. Two words of warning then:

  • Catalyst object inside TT is actually the context object, also known as the $c variable (thanks, Diego)
  • the name of this object is actually a config variable, so it can be changed

Having said this, I’m going to use the “c” convention. More on this topic you can read in the Friendly Manual, and remember to see the Synopsis, where examples for both cases are shown.

DANGER AGAIN, Will Robinson!

However, the helper script for View::TTSite assumes we want to use the “Catalyst” object instead of “c”, so it may be a good idea to fix some files already created. To do this, we have to find them first in the root directory (I assume we are in the helloworld directory). We can suppose that word “Catalyst” will be somewhat popular there, so better idea is to search for this word followed by dot; and since we’re going to use grep, we have to escape the dot:

grep -r 'Catalyst\.' root/
root/lib/config/main: IF Catalyst.debug;
root/lib/config/main: MACRO debug(message) CALL Catalyst.log.debug(message);
root/lib/config/url:[% base = Catalyst.req.base;

In the first file we have to change Catalyst to c, but the second one will be more interesting. According to the Friendly Manual, the “c” convention offers us two aliases: base and name. So, in the end, the root/lib/config/url file can be changed from this:

[% base = Catalyst.req.base;
   site.url = {
     base    = base
     home    = "${base}welcome"
     message = "${base}message"
   }
-%]

to this:

[% site.url = {
     base    = base
     home    = "${base}welcome"
     message = "${base}message"
   }
-%]

And now we can restart the server, but we won’t see any difference, because until now we just set the body. And when Catalyst sees that the body is already set, it assumes it shouldn’t be processed by View, thus passing the raw body directly to the client.

So, to enjoy the beautiful TTSIte view we have to try some quick hack – let’s go to the end of the index action of the Root controller and change it a bit:

    #$c->response->body( $template );
    $c->stash->{template} = 'welcome.tt2';

Colours! We have colours! But wait, why it gives us a link to the nonexisting page? Let’s fix it, adding the message action:

sub message :Local :Args(0) {
  my ( $self, $c ) = @_;

  $c->stash->{template} = 'message.tt2';
}

Now we can refresh the page and voila! But since there’s a form, why not to fill it and submit?

Ouch. Still no message. But if we open the message.tt2 file we will see that it is passed as a parameter called “message”. So there’s no problem:

sub message :Local :Args(0) {
  my ( $self, $c ) = @_;

  $c->stash->{message} = $c->req->params->{message};

  $c->stash->{template} = 'message.tt2';
}

As an exercise for a reader I leave you with changing “Your Name Here” to something more meaningful.

We have just skimmed the topic of View and there’s definitely more to come. But next posts will be probably more focused on the installation of Catalyst and its dependencies.

About these ads

2 Responses to “Catalyst is easy – fourth movement”

  1. Hmm. Is it true? :-)

  2. brunorc said

    Hmm. Can you be more specific?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: