CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need to change browser's title for one page

 



sunrise11
Novice

Apr 11, 2011, 10:40 AM

Post #1 of 27 (6298 views)
Need to change browser's title for one page Can't Post

My script has a sub-routine for all of the pages that outputs the header and footer files for each page.

I want to modify the script so that one page shows a different title in the browser. Right now if people want to save a shortcut to that page, it has a general title and not something that specific for that page.

I'd like the code for the page's title to be: Page for $name that would display in browsers as "Page for Person's Name".

Here is the sub-routine for all generated pages:


Code
### Subroutines ### 

# insert a text file in the result HTML
sub insert {
my $trigger = shift;
if ($trigger eq "header") {
print "Content-type: text/html\n\n";
open(FILE,"<$gHeader") || return 0;
} elsif ($trigger eq "footer") {
open(FILE,"<$gFooter") || return 0;
} elsif ($trigger eq "info") {
open(FILE,"<$gInfo") || return 0;
} else {
return 0;
}


Here is the section of code that starts the page where I want to change the title:


Code
# view wishes of a user (by user or guest) 
sub view {
my $test = &auth($gF{'USERNAME'},$gF{'AUTH'});
if (!$test) { return; }
if (!open(FILE,"<$gDataDir/$gF{'USERNAME'}.ini")) { return; }
chomp($line=<FILE>);
close(FILE);
my (@items);
if (open(FILE2,"<$gDataDir/$gF{'USERNAME'}.gft")) {
@items = <FILE2>;
close(FILE2);
}
my ($fullname,$code,$taken);
($fullname,undef,undef,$code,undef,$seetaken) = split(/:/,$line);
&table_begin;
print "<h3>$gShortName for $fullname</h3>\n";


Is there a way for me to modify that section to output the different title?

Thanks in advance for your assistance.


FishMonger
Veteran / Moderator

Apr 11, 2011, 11:55 AM

Post #2 of 27 (6293 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

Use a template.

http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm


sunrise11
Novice

Apr 11, 2011, 12:09 PM

Post #3 of 27 (6290 views)
Re: [FishMonger] Need to change browser's title for one page [In reply to] Can't Post

I'm a web designer who knows how to edit and customize scripts somewhat, not a programmer.

Therefore, I'm not really savvy with the code.

Which template would best accomplish this purpose?


Zhris
Enthusiast

Apr 11, 2011, 7:12 PM

Post #4 of 27 (6280 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

The template module Fishmonger suggested. It allows you to place a few special tags inside your HTML code, which will be replaced with your chosen values before printing:


Code
<html> 
<head>
<title><TMPL_VAR NAME=TITLE></title>
</head>
<body>



Code
use HTML::Template; 

my $template = HTML::Template->new(filename => 'Header.html');

$template->param(TITLE => 'This is the page title');

print "Content-Type: text/html\n\n", $template->output;


Chris


(This post was edited by Zhris on Apr 11, 2011, 8:00 PM)


sunrise11
Novice

Apr 11, 2011, 8:18 PM

Post #5 of 27 (6264 views)
Re: [Zhris] Need to change browser's title for one page [In reply to] Can't Post

Okay, I entered this code and generated a 500 server error.

And all that generated in the title was: <TMPL_VAR NAME=TITLE>.

Am I supposed to install something to make this work?


(This post was edited by sunrise11 on Apr 11, 2011, 8:21 PM)


Zhris
Enthusiast

Apr 11, 2011, 9:04 PM

Post #6 of 27 (6262 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

use CGI::Carp to display a more explicit error message:


Code
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);


You need to have HTML::Template installed. Its also difficult to diagnose code we can't see.

Chris


(This post was edited by Zhris on Apr 11, 2011, 9:08 PM)


sunrise11
Novice

Apr 12, 2011, 5:07 AM

Post #7 of 27 (6239 views)
Re: [Zhris] Need to change browser's title for one page [In reply to] Can't Post

Okay, I never used the HTML Template system before. I have to do that first.

Is this the correct script to install to my server?

http://sourceforge.net/projects/html-template/


Zhris
Enthusiast

Apr 12, 2011, 6:39 AM

Post #8 of 27 (6226 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

The chances are its already installed on your server. Until you provide a more explicit error message, not sure whats happening. When looking to install new modules, stick to using CPAN or other reputable websites.

Chris


sunrise11
Novice

Apr 12, 2011, 9:22 AM

Post #9 of 27 (6208 views)
Re: [Zhris] Need to change browser's title for one page [In reply to] Can't Post

I really appreciate your quick replies and feedback more than you know.

I am trying to find out if the HTML Template module is installed on my hosting account. I asked a tech who had referred it to a higher tech and will find out in 24 hours or so.

I didn't see it listed with installed libraries. Not sure if it would be listed there.

Question in the meantime - is the the module really needed?

The Perl script that I'm using already accesses HTML just fine. It's hooked up to header and footer HTML files and the home page is an HTML file.

Since that doesn't reference the HTML Template module, I'm not sure why I need it for this purpose.

I guess I need it to be able to insert the Perl code in the header HTML document. Right?


Zhris
Enthusiast

Apr 12, 2011, 9:46 AM

Post #10 of 27 (6203 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

Hi,

Personally I use PHP headers and footers, which I print using a http request. This way I can pass the title to the header, and use PHP to get/print it.

HTML::Template fundamentally searches for the tag inside the HTML and replaces it with your value. If all you need to do is manipulate the title, then a simple search and replace might suffice over using HTML::Template. However, HTML::Template allows for "complex" templating using "in template" loops and so forth, therefore you may find a need for it in more complicated projects in the future. Its good to learn it.

No Perl code goes inside the HTML. The idea is to keep Perl and HTML seperate. Mason would be the closest thing to doing that.

Use the Perl script to bring everything together much like you probably did already, but you still haven't shown your whole code. E.g.:


Code
use HTML::Template;  

my $title = 'This is the page title';
my $templateheader = HTML::Template->new(filename => 'Header.html');
$templateheader->param(TITLE => $title);

my $main = 'This is the main content';

my $templatefooter = HTML::Template->new(filename => 'Footer.html');

print "Content-Type: text/html\n\n";
print "", $templateheader->output;
print "$main";
print "", $templatefooter->output;


Finally, I assume you didn't try to use CGI::Carp (if installed), which would provide you with an error message that could be used to derive whether HTML::Template is installed or not. Or check the error logs if you have access to them.

Chris


(This post was edited by Zhris on Apr 12, 2011, 10:45 AM)


sunrise11
Novice

Apr 12, 2011, 11:02 AM

Post #11 of 27 (6186 views)
Re: [Zhris] Need to change browser's title for one page [In reply to] Can't Post

I like PHP better and have dealt with those scripts as well. PHP is supposed to be more secure than Perl.

The Perl script is an old open-source script that I've had for years. It's very basic but I like it. I tweaked it over the years to improve it and now I want to make this other improvement to it.

Stupid question but I'll ask anyway. Is it possible to put a PHP header in the Perl script?

Can't I just insert the new title code into the Perl script without using the HTML Template module?

I have a script that lets me view what's installed on the server but the HTML Template module didn't show up.

Maybe I'll send you the script and you can see what I'm dealing with rather than post the whole thing here.


Zhris
Enthusiast

Apr 12, 2011, 11:19 AM

Post #12 of 27 (6184 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

PHP being more secure than Perl is just an oppinion. Both languages will require you to validate i.e. form data in much the same way. The statistics show that in relation to the number of PHP users / Perl users, there are still more security issues arrousing with PHP, but that could just be down to chance.

You have to keep in mind, that in order for PHP code to run, the PHP "file" will need the PHP extension .php. Its not as simple as being able to put PHP code inside Perl code, although there may be modules available to help.

Heres a very basic example how I usually build a webpage, although it depends on how much traffic I expect:

Create 3 files:
Header.php
Content.html
Footer.php

Create a Perl script to "glue" the files together and print:


Code
use LWP::Simple; 

my $header = "Header.php?Title=mytitle";
my $content = "Content.html";
my $footer = "Footer.php";

print "Content-type: text/html\n\n";
print get($header);
print get($content);
print get($footer);


The Header.php will receive a param variable "Title" e.g.:


Code
<?php 
if (isset($_GET['Title'])) {
$title = $_GET['Title'];
}
else {
$title = 'Default title';
}
?>

<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>


Thats how I usually use PHP in my Perl scripts, by literally sending a request and printing its output.

Chris


(This post was edited by Zhris on Apr 12, 2011, 1:34 PM)


sunrise11
Novice

Apr 14, 2011, 6:18 AM

Post #13 of 27 (6157 views)
Re: [Zhris] Need to change browser's title for one page [In reply to] Can't Post

Chris,

Thanks to you, I'M VERY CLOSE!!!

I'm getting <?php echo $title ?> in the browser's title so the PHP directive isn't working quite yet.

I have it set up a little differently than your way because I didn't want to modify the original code too much if possible.

Here's how it's set up (with my changes in bold):

header.php


Code
<?php  
if (isset($_GET['Title'])) {
$title = $_GET['Title'];
}
else {
$title = 'My Documents';
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>

<title><?php echo $title ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name='description' content='Description' />
<meta name="keywords" content="Keywords" />
<link href="file.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div><center>
<img src="header.gif" alt="header" border="0" />
</center>
</div>


Perl Code (with my changes in bold):

L43 =

Code
# This file is the header to be displayed on all pages. $gHeader = "my-root-directory-here/header.php";


L81-101 =

Code
### Subroutines ### 

# insert a text file in the result HTML
sub insert {
my $trigger = shift;
if ($trigger eq "header") {
use LWP::Simple;
my $header = "header.php?$title";

print "Content-type: text/html\n\n";
open(FILE,"<$gHeader") || return 0;
print get($header);
} elsif ($trigger eq "footer") {
open(FILE,"<$gFooter") || return 0;
} elsif ($trigger eq "info") {
open(FILE,"<$gInfo") || return 0;
} else {
return 0;
}
while (<FILE>) { print; }
close(FILE);
return 1;
} # insert



L260-262 =

Code
# view list of a user (by user or guest) 

use LWP::Simple;

my $header = "header.php?Title=$fullname\'s Document";
print "Content-type: text/html\n\n";
print get($header);


sub view {
my $test = &auth($gF{'USERNAME'},$gF{'AUTH'});
if (!$test) { return; }


L260-262 is where I want the title to be different than the other pages so if users save a shortcut to the page, they will have a specific reference (ex: Mary's Document). The other pages are generated from L81-101 (subroutines).

Do you see any reason why the title wouldn't output properly using this method?

Is there a conflict between $header and $gHeader?


Code
# insert a text file in the result HTML 
sub insert {
my $trigger = shift;
if ($trigger eq "header") {
use LWP::Simple;
my $header = "header.php?$title";
print "Content-type: text/html\n\n";
open(FILE,"<$gHeader") || return 0;
print get($header);
} elsif ($trigger eq "footer") {


I'm so close!


(This post was edited by sunrise11 on Apr 14, 2011, 8:57 AM)


Zhris
Enthusiast

Apr 14, 2011, 6:33 AM

Post #14 of 27 (6153 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

Hi,

I've got to go into work for an hour, ill take a look when I get back.

Chris


sunrise11
Novice

Apr 14, 2011, 6:36 AM

Post #15 of 27 (6151 views)
Post deleted by sunrise11 [In reply to]

 


Zhris
Enthusiast

Apr 14, 2011, 9:40 AM

Post #16 of 27 (6121 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

Hi,

Theres a few different aspects that need clarrifying.

L81-101:
$header and $gheader are inevitably different variables, therefore if you set $header with your php header, then use $gheader to open the file, its not going to open $header (which is a full URL not a path anyway). I explained in a previous post that in order for PHP to compile, the PHP code must be executed as php. If you open the file in perl, read it, and print its contents then the PHP isn't going to compile, instead it will compile as HTML code. You will have to use the same method whether you want to use a new title, or the default title, to be most efficient.

L260-262:
Thats more like it, I can't see that you are doing anything wrong here. This should work, although you may need to encode the URL. You should use this snippet of code even when you want to print the default title, just leave the value of the Title attribute empty, and the PHP script will revert to the default title.

Before trying to implement the new code into your existing code, how about creating a fresh Perl script, just to test it all out first, which you can play around with and see how it works. With this new method, you would no longer need to use open to read the header, you can simply use LWP::Simple's get method to print the output of the compiled PHP. When you are confident you understand how it works, you will have a much easier time in implementing it, and removing any unneccessary old code.

Chris


sunrise11
Novice

Apr 14, 2011, 12:03 PM

Post #17 of 27 (6106 views)
Re: [Zhris] Need to change browser's title for one page [In reply to] Can't Post

Your method suggests using three files: header.php , body.html and footer.php.

However, this script just pulls a header and footer file and the body (information) is dynamically generated from the script.

Does your method have to point to a separate body file?


Zhris
Enthusiast

Apr 14, 2011, 12:21 PM

Post #18 of 27 (6105 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

Nope not at all. I used "Content.html" as part of the example. Instead you would replace this with the bits generated by your Perl script.


Code
#Header 
print get($header);

#Body
my $string = "hello";
my @array = split //, $string;
print "$_<br />" foreach (@array);

#Footer
print get($footer);


Chris


(This post was edited by Zhris on Apr 14, 2011, 12:22 PM)


sunrise11
Novice

Apr 15, 2011, 9:50 AM

Post #19 of 27 (5995 views)
Re: [Zhris] Need to change browser's title for one page [In reply to] Can't Post


Quote
$header and $gheader are inevitably different variables, therefore if you set $header with your php header, then use $gheader to open the file, its not going to open $header (which is a full URL not a path anyway)... If you open the file in perl, read it, and print its contents then the PHP isn't going to compile, instead it will compile as HTML code.


Is there a way to compile this same effect in Perl without using PHP?

So that the existing $gheader will print the desired title?


(This post was edited by sunrise11 on Apr 15, 2011, 9:52 AM)


Zhris
Enthusiast

Apr 15, 2011, 10:39 AM

Post #20 of 27 (5986 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

The solutions were already provided above, because somehow you need to "dynamically" change the title.

Use HTML::Template
or
Search and replace the header
or
Pass a "title" variable to PHP or another server side script.

Chris


sunrise11
Novice

Apr 15, 2011, 3:49 PM

Post #21 of 27 (5969 views)
Re: [Zhris] Need to change browser's title for one page [In reply to] Can't Post

Okay, I'll give the HTML Template another try.

For clarification, should this go within the Perl script and not a separate HTML doc:


Code
<html>  
<head>
<title><TMPL_VAR NAME=TITLE></title>
</head>
<body>


Before or beneath?


Code
use HTML::Template;  

my $template = HTML::Template->new(filename => 'Header.html');
$template->param(TITLE => 'This is the page title');
print "Content-Type: text/html\n\n", $template->output;


Thanks for your patience.


(This post was edited by sunrise11 on Apr 15, 2011, 3:50 PM)


FishMonger
Veteran / Moderator

Apr 15, 2011, 4:21 PM

Post #22 of 27 (5963 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

The html code is in a normal html file which uses additional tags that the Perl module utilizes. The Perl script sets template variables which the module uses to replace the special html tags that are in the html template.


sunrise11
Novice

Apr 27, 2011, 11:38 AM

Post #23 of 27 (5380 views)
Re: [FishMonger] Need to change browser's title for one page [In reply to] Can't Post

Okay, I'm ready to tackle this project again after a break.

My next newbie question Blush is, can I mix the HTML template with the original code so that only the header file is using the template and not the body or footer?

Here's an example:


Code
# insert a text file in the result HTML 
sub insert {

use HTML::Template;
my $template = HTML::Template->new(filename => 'header.html');
$template->param(TITLE => 'My chosen title here');
print "Content-Type: text/html\n\n", $template->output;

} elsif ($trigger eq "footer") {
open(FILE,"<$gFooter") || return 0;
} elsif ($trigger eq "info") {
open(FILE,"<$gInfo") || return 0;
} else {
return 0;
}
while (<FILE>) { print; }
close(FILE);
return 1;
} # insert


Or, if I go with a php header, can I mix it up so that the header gets called with the php script and the body and footer get called as per the original script?

Thanks!


(This post was edited by sunrise11 on Apr 27, 2011, 12:08 PM)


FishMonger
Veteran / Moderator

Apr 27, 2011, 11:42 AM

Post #24 of 27 (5378 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

No, it won't work like that.


sunrise11
Novice

Apr 27, 2011, 12:28 PM

Post #25 of 27 (5369 views)
Re: [FishMonger] Need to change browser's title for one page [In reply to] Can't Post

It looks like your suggestion of Pass a "title" variable to PHP or another server side script is my best option if I want to simply change the browser title and not rewrite the script.


FishMonger
Veteran / Moderator

Apr 27, 2011, 12:33 PM

Post #26 of 27 (1654 views)
Re: [sunrise11] Need to change browser's title for one page [In reply to] Can't Post

It's unfortunate, but when working with spaghetti code like this, you often have to either start over from scratch and design it correctly or add more spaghetti code.


(This post was edited by FishMonger on Apr 27, 2011, 12:34 PM)


sunrise11
Novice

Apr 27, 2011, 1:00 PM

Post #27 of 27 (1649 views)
Re: [FishMonger] Need to change browser's title for one page [In reply to] Can't Post

Yes, it's definitely old spaghetti code.

Now I see that "new" or "correct" code won't mix with the spaghetti code so I'll have to figure out how to generate the title page in spaghetti code.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives