1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Common Header and footer using HTML::Template

Discussion in 'Perl' started by Rakish, Jan 30, 2007.

  1. Rakish

    Rakish New Member

    Joined:
    Jun 30, 2006
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Hello all,

    I am using HTML::Template to separate the design from logic. As of now I am using html header and footer in all template pages as under:

    Code:
    <div id="header">
    
    <h1><a id="bigtext" title="Go to home page" href="home.pl">TITLE</a></h1>
    	<div id="links">
    	<text2 id="text2">Logged in as <TMPL_VAR NAME=USERNAME></text2>
    		<a href="get_email.pl">Invite</a> |
    		<a href="help.pl">Help</a> |
    		<a href="logout.pl">Logout</a>
    	</div>
    </div
    
    but I want to have this code in one file and want to include that file in all the pages.

    Any suggestions will be appreciated.

    Rakish :)
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Here is an example:

    The template file:
    HTML:
    <html>
     <head><title>Test Template</title></head>
      <body>
      <h3>My Header
      
      <TMPL_LOOP NAME=LP>
      <font color=green>Name :  
      
      Dept : <TMPL_VAR NAME=DEPT> 
      
      </TMPL_LOOP>
      <br/>
      <center>This is my footer
      </body>
      </html>
    The Perl file:
    Code:
    #!/usr/bin/perl
      
      use CGI;
      use CGI::Carp /warningsToBrowser fatalsToBrowser/;
      use HTML::Template::Compiled compatible => 1;
      use DBI;
      
      $q = new CGI;
      my $template = HTML::Template::Compiled->new(filename => 'tpl/test.tpl', die_on_bad_params => 0);
      @loop_data = ();
      
      $loop_data[0] = {'EMP_NAME'=>'Pradeep','DEPT'=>'Programming'};
      $loop_data[1] = {'EMP_NAME'=>'Manindar','DEPT'=>'Accounts'};
      $loop_data[2] = {'EMP_NAME'=>'Asha','DEPT'=>'Marketing'};
      $loop_data[3] = {'EMP_NAME'=>'Tanaz','DEPT'=>'Programming'};
      
      $template->param(LP=>\@loop_data);
      
      print $q->header,$template->output;
     

Share This Page