Who
is this for
This article is for those who would like to learn
about HTML::Mason Components
Introduction One of the
nice things about HTML::Mason is that it allows you to create
components. Components are basically chunks of code that are
re-useable by other HTML::Mason scripts.
Let's say that you want to put a
header and footer on all the pages on your website. You can code the
header and footer in all the pages. If you want to change something
in the header or footer, you will have to go through all the pages
and put the change in.
A simpler way to do this is to
seperate the header and footer into components.
Components Let us say
that you want to put a welcome message that displays the date as a
header to all the pages. Your header.html will look something like
this:
<%perl> my @date = localtime; my $hello = 'Morning'; if ($date[2] > 12 and $date[2] < 18) { $hello = 'Afternoon'; } elsif ($date[2] >= 18) { $hello = 'Evening'; } </%perl> Good <% $hello %><br/> Today is: <% $date[5] + 1900 %>/<% $date[4] + 1 %>/<% $date[3] %>
|
All your pages have to include now
is a call to the header component:
or
% $m->comp('header.html');
|
Assuming you ran this in the
evening, this produces the following output:
|
Good Evening Today is: 2003/9/29
|
Passing Parameters to
Components
You can also pass parameters to
components. You do this similar to passing a parameter to any Perl
function. If you want to pass a name to your component, you can do
this:
<& header.html, name=>'philip' &>
|
Your component now should be
programmed to pick this parameter up. You can do this in two ways:
The first way is to receive the
parameter as if you were receiving it from a Perl function: using the
@_. In this case, the contents of @_ will be ('name', 'philip'). Try
this:
<%perl> my @date = localtime; my $hello = 'Morning'; if ($date[2] > 12 and $date[2] < 18) { $hello = 'Afternoon'; } elsif ($date[2] >= 18) { $hello = 'Evening'; } </%perl> Good <% $hello %><br/> Today is: <% $date[5] + 1900 %>/<% $date[4] + 1 %>/<% $date[3] %> % foreach (@_) { <br><% $_ %> % }
|
The result of the code is:
Good Evening Today is: 2003/9/29 name philip
|
or you can do this:
<%perl> my @date = localtime; my $hello = 'Morning'; if ($date[2] > 12 and $date[2] < 18) { $hello = 'Afternoon'; } elsif ($date[2] >= 18) { $hello = 'Evening'; } </%perl> Good <% $hello %><br/> Today is: <% $date[5] + 1900 %>/<% $date[4] + 1 %>/<% $date[3] %> <B><% $name %></b> <%args> $name </%args>
|
The result will be:
Good Evening Today is: 2003/9/29 philip
|
The <%args> section
allows use the key to name the variable. In our example, the key of
the hash was name so the variable name is $name. The difference
between using the <%args> section and the @_ variable
is that the variables in the <%args> section are
mandatory. The component will raise an error if the variable is not
passed to the component. In our example, if the name=> 'philip'
is not passed, the component will encounter an error. If you used @_,
it will not cause an error.
If you want to know more about
HTML::Mason components, you can read the HTML::Mason::Devel man page.
|