Writing the handlers

We have several tag handlers to write. The include, image_list, image_row, and image handlers are what our template needs. Each one has a particular task. The include handler will just include a file. The image_list handler calls our action plugin and passes the result on to it's children handlers. The image_row handler outputs our images in rows according to our number attribute. And finally the image handler outputs the specific information for each image. Lets get started on them shall we.
package image_list;
use lib::common::template_handler;
use base qw(template_handler);

sub run {
my $self = shift;
my $Params = shift;
    
my $content = $self->block();
my $result = $self->data();
my $parsed = $self->parse_block(undef, $result)
or $self->errors("failed running sequencer on $self->{Token}->{tagname} tag", "log");
    
return $parsed;
}

return 1;
As you can see there usually isn't a whole lot complicated to a tag handler. They just take data and pass it on or output. This particular handler retrieves the image array and passes it on to it's children tags that it runs the sequencer on. Then it returns the result of the sequencer. Now we need to write the next handler
package image_row;
use lib::common::template_handler;
use base qw(template_handler);

sub run {
my $self = shift;
my $image_array = shift;
    
my $content = $self->block();
my $loop =  $self->attributes()->{number};

my $parsed;
my $counter = 0;
    
    # loop as many times as our number attribute indicates
foreach (@$image_array) {
  $parsed .= $self->parse_block(undef, $result)
  or $self->errors("failed running sequencer on $self->{Token}->{tagname} tag", "log");
  $parsed .= "<br />" if $counter = 4;
  $counter .= 0 if $counter = 4;
  $counter++;
}

return $parsed;
}

return 1;
This tag is only slightly more complicated than it's predecessor. It takes the array passed as a parameter and sends each element on to the children tags also outputting a <br> tag every 4 images. We only have one tag handler left. The image tag handler.
package image_row;
use lib::common::template_handler;
use base qw(template_handler);

sub run {
    my $self = shift;
    my $image = shift;
    my $App = $self->app();
    my $url = $App->{Env}{dir};
    
    my $attribute = $self->attributes()->{attr};
    if ($attribute == "location") {
      my $parsed = $url . "/$image";
    } elsif ($attribute == "location") {
      my $parsed =  "Image Name: $image";
    }
    
    return $parsed;
}

return 1;
This handler takes our location passed on to it and outputs a url and an alternate text based on which attribute this particular handler has. This was our last handler so now we can start using our new gallery application.
Jeremy Wall 2006-08-21