Gossamer Forum
Home : Products : Links 2.0 : Discussions :

using Perlsubroutines in Templates

Quote Reply
using Perlsubroutines in Templates
Hi!
It seems, that I can use Tags in Templates for calling subroutines. I must asign a subroutine to the Variable, and insteed of printing the contents of the var, links executes the code in the subroutine! Seems easy. But doesnīt work. One error seems with links, it searchs for 'CODE', but perl sets the var to 'CODE(something)', I fixed that! But now I donīt know how I should assign the subroutine to the var, so links can execute it! (

Please help me!

bye
Larpie

------------------
Quote Reply
Re: using Perlsubroutines in Templates In reply to
To the best of my recollection, this is how it is done. Remember, I am doing this from memory, so it may not be quite right.

Let's assume you have a subroutine called "sub do_something" and you want to call it in a template. In the subroutine of site_html_templates.pl that loads the template, you need to include something like the following in the list of variables the template is allowed to use:

Quote:
my_routine => \&do_something,

Then in the templace, you can use <%my_routine%> to execute the subroutine. You may need a single- or double-quote set around the "\&do_something" but I am not sure. Try with and without and see what happens.

I hope this helps.
Quote Reply
Re: using Perlsubroutines in Templates In reply to
Thank you for your help! But know perl says

Code:
Undefined subroutine &main:: called at /var/www/cgi-bin/libs/templates.pm line 32.
<HR></BLOCKQUOTE>

(I wrote my own script for handling templates, using the Links-Code as core) Wink

Whats wrong now?

bye
Larpie
Quote Reply
Re: using Perlsubroutines in Templates In reply to
Line 32 of Templates.pm does not contain a call to a subroutine; it contains the following code:

Code:
my %errors = (
'CANTINIT' => "Template: Can't init, no root defined, or not a hash ref: '$e1'",
'NOTEMPLATE' => "Template: Can't find template: $e1",
'CANTOPEN' => "Template: Can't open template: $e1. Reason: $e2",
'NONAMESPACE' => "Tempalte: You haven't loaded a namespace yet!",
'NONAME' => "Template: No name was defined for this template!",
'NOSTRING' => "Template: No text was defined for template: $e1"
);

If you wrote your own script to handle the templates, I can't help you because I don't know what you wrote. I can only advise based on what is in the links2.zip archive.

[This message has been edited by Bobsie (edited April 16, 1999).]