Perl Beginners - Addressbook Tutorial Step 5 - Adding the start method and our first HTML::Template file |
In Step 4, we created the unrestricted Perl handler. The handler is the class called by the cgi to handle requests. In this step, we'll add a method to the handler to load the default search form. |
What should this method do? We decided to search on several fields in different tables. Theoretically, we could provide regular text form inputs and have the user type in their search criteria. Or, we could provide <select> pull-downs to ease the search request. We may want to provide a pull-down with all the person names in it. But, if the database has thousands of names, this may be too tedious. What about a pull-down with all the city names? Again, there may be hundreds of different cites, so this could be tedious. How about a pull-down with all the area codes? That may not be so bad. And another one with the state names? Well, the most it could have would be 50 (52 if you count Puerto Rico and Washington D.C., I guess), so that doesn't seem too bad either. |
So we'll need the start method to do the following: |
Before we write the method, let's go over how HTML::Template works. In a nutshell: |
|
After the use declaration, we construct a reference to a new HTML::Template object, passing in the name of the template file (somefile.tmpl) we'll use. We then set up an array of hash references. Each hash ref contains the last name, first name, band name, and occupation of each person. Then, we set up a parameter hash that contains the dynamic data to be displayed in the template. In the parameter hash, we have a scalar value (GENRE), and a reference to an array (PEOPLE). |
Here is what a corresponding template (somefile.tmpl) file may look like: |
|
Notice the scalar value in the parameter hash (GENRE) is named directly, but the array reference (PEOPLE) is named inside a <TMPL_LOOP>. <TMPL_LOOP> repeats whatever is between it and the closing </TMPL_LOOP> for each reference in the array, filling each <TMPL_VAR> with the values in each hash reference. |
It's a good idea to always use either the ESCAPE=HTML or ESCAPE=URL attribute in any <TMPL_VAR> tag. For example, assume we have a form input box where we pre-fill it with a length value: |
|
Assume the length value is 90". Therefore, the final HTML will look like this: |
|
Without the ESCAPE=HTML, the final HTML would look like this: |
|
And it's unlikely any browser the HTML is displayed in will be able to grok that. Simply put, the ESCAPE=HTML attribute changes any occurrence of &, ", >, and < to &, ", >, and <, respectively. Similarly, the ESCAPE=URL will change ' ' and '/' to '+' and '%2F'. |
So the final HTML output for our example will look like this: |
|
So let's write the start method, which we'll add to Handler.pm: |
|
First, we use our utility class Record.pm (which we have not written yet). Inside the start method we shift in the $self reference. Then, we construct an object reference to the utility class. We put the AREA_CODES and STATES values into the template parameter hash and set the output file. |
The corresponding template file (search_form.tmpl) will look something like this: |
|
Notice we're declaring in a hidden form value called action. When this form is submitted, it'll call the search method to process the form, search for matching records, and display the results (we'll write this method later). |
So what about the utility class, Record.pm? This class will provide the select_distinct_area_codes and select_distinct_states methods to Handler.pm. What will this utility class need to function? It will need: |
|
We'll write these two classes in the next step. |
Coming next - Writing the utility and SQL classes, and using Class::MethodMaker. |
Copyright © 2001 by Peace Computer Systems |