|Written by Philip L Yuson|
is this for
This article is for those who would like to learn about HTML::Mason Components
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.
All your pages have to include now is a call to the header component:
Assuming you ran this in the evening, this produces the following output:
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:
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:
The result of the code is:
or you can do this:
The result will be:
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.