Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Passing hash to global in template?

Quote Reply
Passing hash to global in template?
hi,

can someone tell me how to pass a hash to a global function in a template tag?

what i've currently got is this, but it's not being passed in as a hash for some reason...

Code:
<%mytag ({ row => 5, cols => 20})%>


thanks

regan.

Last edited by:

ryel01: Jun 15, 2002, 2:03 PM
Quote Reply
Re: [ryel01] Passing hash to global in template? In reply to
Don't use references, do:

<%mytag( foo => 1, bar => 2 )%>

and then in your global:

my %hash = @_;
print $hash{foo}, $hash{bar};

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Passing hash to global in template? In reply to
I need a global to check if a plugin is installed or not, so decide to execute the tag in the template or not.
This is how I would like to do:
Code:
<%if check_plugin( 'foo' => '1', 'bar' => '2' )%>
<%Plugins::myplugin::function%><%endif%>

I also checked your code, and works fine:
Code:
<%mytag( foo => 1, bar => 2 )%>

and then in your global:

my %hash = @_;
use Data::Dumper;
print "<br>\n hash: " . Dumper(\%hash) . "<br>\n";
This works fine. Hash content will be: { 'foo' => '1', 'bar' => '2' }.

But if I place an 'if' statement before the global call, then will not print the hash as earlier Frown:
Code:
<%if mytag( foo => 1, bar => 2 )%>
Hash content will be: { 'HASH(0x21cd8c8)' => undef }.

Why? What happens if I use 'if' statement? Does is pass arguments other way?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Passing hash to global in template? In reply to
Hi,

I know about this problem with the template parser - basically, it needs some parser overhauling to compartmentalize and break down much of the parsing. We did try and hack that in at one point, but it ended up causing several failures in our template test script so unfortunately we had to remove it.

Right now, the best solution to your problem is to do this in two steps:

<%set have_plugin = check_plugin(foo => 1, bar => 2)%>
<%if have_plugin%><%Plugins::myplugin::function%><%endif%>

Also note that you can sort of call subroutines in an if, like this:

<%if Package::function%>

You can get away with passing arguments this way, but if you do so, be warned that it doesn't always work 100% correctly - if you have the word "and" or "or" in there, calling it that way will break things. By far the safest and most worry-free way when you need to pass arguments is to use the 'set', as I showed above.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] Passing hash to global in template? In reply to
Thanks Jason, that you showed me the workarounds! Cool

Could you collect these KNOWN BUGS into a separate forum, similar to Official Bug Fixes forum?
This bug announce forum could be in category 'Gossamer Threads Inc./Links SQL', named as 'Official Known Bugs' forum. Also for each bug should be defined, if there is a workaround or not.
IMHO would be VERY useful.

Opinion?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [Jagerman] Passing hash to global in template? In reply to
Jason,

Unfortunately the following solution is not working Frown :
<%if Plugins::myplugin::function%>
<%Plugins::myplugin::function%>
<%endif%>

Does it work for you?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Dec 13, 2002, 6:50 AM