Gossamer Forum
Home : Products : Gossamer Links : Discussions :

How can I add a variables like $User/user $CFG/config which are available everywhere

Quote Reply
How can I add a variables like $User/user $CFG/config which are available everywhere
Hi,

I am a little stuck. I have some variables within a Plugin like special paths. At the moment they are within the init subroutine hook and I add information to $CFG like:

Code:
$CFG->{date_to_text_long} = ...
$CFG->{products_db} = ...

So in any Perl code after having called init I can use the variables and within any template i can use them as well.

It should be easy to do this with $my_own_name_for_this but I donīt get it.
The problem with the above is that the variables are dynamic and can vary from visitor to visitor but they are stored in Config.pm every time they are used.
Though it works that way it seems not reasonable to do it like that.

Thanks for hints

Regards

n||i||k||o

Last edited by:

el noe: Jun 3, 2011, 4:54 AM
Quote Reply
Re: [el noe] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
In glinks 3.3.0, we added a %STASH global which would be useful in situations like this. It gets reset on each request, but is globally available. I would recommend against setting things inside $CFG, since as you have noticed, it can get saved into your Config/Data.pm file.

Adrian
Quote Reply
Re: [brewt] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Hi Adrian,

Thank you very much for your reply and hint.
I tried to define some variables in %STASH within a Plugin hooking to init.
I cannot see how to make them available in scripts and templates.
I tried a Plugin hook:

Code:
sub init {
my (@args) = @_;

$STASH{test} = "I was here!";
return @args;
}

... and it did not work. I thought a problem might be this part of sub init { within Links.pm

Code:
$PLG->dispatch('init', sub {});

(%STASH, $GLOBALS, $LANGUAGE, $USER) = ();
}
I changed it to:
Code:
$PLG->dispatch('init', sub {});

(%STASH, $GLOBALS, $LANGUAGE, $USER) = ();
$STASH{anothertest} = "...and here";
}

The code did not make anything show up neither in a perl script nor in a template (I used Data Dumper for %STASH which is empty, $STASH{test), $STASH{anothertest} and <%STASH.test%>).

It would be great to get another hint.

I thought of using $IN as well but I do not like this one either but maybe there is no problem:
Code:
sub init {
my (@args) = @_;
$IN->{mystash} = {};
$IN->{mystash}->{test} = "... and here as well";
return @args;
}
I guess the code is just dynamically and cannot be overwritten by params. So it might be better than using $CFG but %STASH seems to be more reasonable.

Regards

n||i||k||o

Last edited by:

el noe: Jun 8, 2011, 3:27 AM
Quote Reply
Re: [el noe] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
If you wanna use $IN, you could do it with:

$IN->param("field" => $val );

..and then access it via $IN->param("field")

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Hi Andy,

Thanks for replying.
What is the advantage of

Code:
$IN->param("field" => $val );

print $IN->param("field");

against

Code:
$IN->{field} or $IN->{mystash}->{field} = $val;

print $IN->{field};
?

Might it not be risky to use param() which would make it possible to define values by calling them in the cgi-URL?
In my opinion by undefining the mystash part I could avoid that.
Code:
$IN->{mystash} = {};

Thanks

n||i||k||o
Quote Reply
Re: [el noe] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Hi,

Yeah, passing them in as standard params could potentially be danagerous (although I can't think of any ways this could be a security issue)

For the %STASH bit, are you using this in your own .cgi script, or from the general scripts? As long as the .cgi has:

Code:
use Links qw/:objects/;

...then you should have access to:

Code:
'objects' => [qw/$IN $CFG $DB $USER $PLG %STASH VIEWABLE/],

..so simply doing:

Code:
$STASH{foo} = "test";

...and then accessing it from another script/function with:

Code:
print "We have a value of $STASH{foo} in this value...";

Then that should work fine (I've done similar stuff with GForum, when you didn't wanna pass it along to the $IN, but needed access to it in another file/function

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Hi Andy,

I would like to use it in new and existing scripts which should be possible with :objects and I have:
Code:
sub init {
my (@args) = @_;

$STASH{test} = "I was here!";

return @args;
}
in /Plugins/Test.pm

install_hooks: 'init', 'POST', 'Plugins::Test::init'

and test.cgi with:

Code:
use lib '.../admin';
use Links qw/:objects/;
local $SIG{__DIE__} = \&Links::fatal;
Links::init('.../admin');
print $STASH{test}

which wonīt print "I was here!". No idea what I am doing wrong or how I could get the %STASH values in templates.

Thanks

n||i||k||o
Quote Reply
Re: [el noe] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Mmm, weird! What happens if you do a Dumper() of %STASH ? I would offer to do a quick test myself, but afraid I'm a little bust atm. If you still havn't figured it out by this evening, I'll have a quick test and see if I can get it working

(BTW, you are using GLinks 3.3, not 3.2?)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Hi Andy,

I am using 3.3 and I guess one problem is in Links.pm the
Code:
$PLG->dispatch('init', sub {});

(%STASH, $GLOBALS, $LANGUAGE, $USER) = ();
}
part in sub init. That means if i put
Code:
$STASH{var} = "val";

after the above the Dumper shows the variable. I havenīt seen that before but itīs one step further.
Probably I cannot add variables to %STASH via hooking into init but I will keep on looking in Links.pm.
I still have no idea how to access STASH in the templates. So thanks very much for your help till now but I still do appreciate hints.

Regards

n||i||k||o
Quote Reply
Re: [el noe] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
I looked at

sub user_page {

in Links.pm which contains:
Code:
# Set up config.*, in.*, user.* template tags:
$vars->{config} = $CFG;
$vars->{in} = $IN;
$vars->{user} = $USER;
and no Plugin hook except
Code:
$PLG->dispatch('clean_output', \&clean_output, \$output, $vars->{page_id});
So as far as I can see I cannot manage to get %STASH available in templates without calling a routine from within templates and %STASH might not be easy to be defined via a Plugin hook within sub init.
I am tending to use the $IN->{mystash} way from abouve to solve this issues. Still happy to hear any comments.

Regards
n||i||k||o
Quote Reply
Re: [el noe] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
This change isn't in 3.3.0, but it's something I committed after:

Code:
--- Links.pm 2009/05/09 17:01:33 1.227
+++ Links.pm 2009/06/04 03:03:21 1.228
@@ -122,9 +122,9 @@
);
}

- $PLG->dispatch('init', sub {});
-
(%STASH, $GLOBALS, $LANGUAGE, $USER) = ();
+
+ $PLG->dispatch('init', sub {});
}

sub init_date {
@@ -476,7 +476,7 @@
$opts->{compress} = $CFG->{compress} unless defined $opts->{compress};
$opts->{print} ||= 0;
$opts->{inheritance} = 1 unless defined $opts->{inheritance};
- my $output = GT::Template->parse($file, [$IN, $GLOBALS, $USER || (), $vars], $opts);
+ my $output = GT::Template->parse($file, [$IN, $GLOBALS, $USER || {}, $STASH{tpl_vars} || {}, $vars], $opts);
if ($dynamic and !$opts->{print} and $IN->param('d')) {
$PLG->dispatch('clean_output', \&clean_output, \$output, $vars->{page_id});
}

It moved the init hook to be be after the resetting of the globals, and also pushes $STASH{tpl_vars} into the templates.

Adrian

Last edited by:

brewt: Jun 8, 2011, 4:12 PM
Quote Reply
Re: [brewt] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Hi Adrian,

Works perfect.
I am using $STASH{tpl_vars}->{whatever} for everything I might need in templates an $STASH{whateverelse} for needful things in scripts.
I made your changes manually so if there is an update of Links.pm it would be great if this part was there as well.

Thanks again to you and Andy

n||i||k||o
Quote Reply
Re: [brewt] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Hi Adrian,

if there is time, could you please update the download file with this update?
I am doing that manually at the moment.

Thanks

n||i||k||o
Quote Reply
Re: [el noe] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Pretty busy lately, but when I get a chance, I'll see what I can do.

Adrian
Quote Reply
Re: [brewt] How can I add a variables like $User/user $CFG/config which are available everywhere In reply to
Hi Adrian,

I can imagine that, but happy to hear, thanks a lot.

Cheers

Niko

Last edited by:

el noe: Jan 30, 2014, 10:22 PM