Gossamer Forum
Home : Products : Links 2.0 : Discussions :

perl code in template files?

Quote Reply
perl code in template files?
All the pages on my site has an ASP snippet that opens a file, reads the content (a number) and writes the number + 1 to the file (a global counter, kind of). And of course displays the number on the page.

Is it possible to add some perl code to the template files for the *.cgi files that does the above?

Quote Reply
Re: perl code in template files? In reply to
Very rudimentary "Perl" codes....

Look at the Enhanced Template.pm Mod located in the Resources Center.

You can also add PERL codes in the subroutines in the site_html_templates.pl and then reference those PERL codes via template specific variables or global variables. EXAMPLE: Look at the WebAdverts code hack referenced in the Links 2.0 Customization Forum.

Good luck....and next time, search the forum and resources section of this site before posting questions...many of them are already answered!

Regards,

Eliot Lee
Quote Reply
Re: perl code in template files? In reply to
Ok. I can't access the server until the FTP is up on monday, so I'll just ask if this is going to work:

- I "enhanched" the template.pm
- I added this code to site_html_templates.pl:
Code:
sub add_counter {

if ( open (HITS_RD, "d:/webserver/cosmo-web/site/counter/count.txt") ) {
$hits = <HITS_RD>;
close HITS_RD;

if ( open (HITS_WR, ">d:/webserver/cosmo-web/site/counter/count.txt") ) {
print HITS_WR ++$hits;
close HITS_WR;
}
}

$counter1 = $hits + 68349;
$counter2 = $hits + 120000;

$cosmorand = rand;
}
I added &add_counter; below the &html_print_headers; line in all subs that generate the CGI-pages.

Now, can I add things like:
Code:
counter 1 is <%counter1%>, and <%cosmorand%> is a random number
to my CGI template files (add.html, etc.)? Or have I misunderstood?

Quote Reply
Re: perl code in template files? In reply to
In Reply To:
I added &add_counter; below the &html_print_headers; line in all subs that generate the CGI-pages.
Are you using TEMPLATES or NOT? Sounds to me like you are NOT using templates and just the site_html.pl, which means that using PERL codes in the "template" files is a mute point, don't ya think!

Regards,

Eliot Lee
Quote Reply
Re: perl code in template files? In reply to
Oh, I AM using templates...

Quote Reply
Re: perl code in template files? In reply to
Well, you should simply create a GLOBAL tag out of the &add_counter sub.

Like the following (and also like the WebAdverts modification referred many times in the Links 2.0 Customization Forum!):

Code:

counter => \&add_counter


in the %globals hash in the site_html_templates.pl file.

Then use <%counter%> in your template files.

Regards,

Eliot Lee
Quote Reply
Re: perl code in template files? In reply to
Thanks for the help so far. If (when?) I get some trouble, you'll hear more from me.... ;-)

Thanks again!


\a