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

Code to get globals working in a custom script?

Quote Reply
Code to get globals working in a custom script?
We had a custom map searching plugin written a while ago, and now we are trying to do some enhancements. The only problem is that we are unable to use globals in the templates this plugin uses. What must I do to get the globals working?
Quote Reply
Re: [Coert] Code to get globals working in a custom script? In reply to
Can you explain a bit more? You don't give us much to go on Whistle

Quote:
unable to use globals in the templates this plugin uses.

What template? Part of the Glinks templates, or a different system alltogether?

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] Code to get globals working in a custom script? In reply to
OK, I'm going to try and give some more details. I'm referring to the Glinks templating system when I talk about templates.


The finder plugin uses its own .cgi file (let's call it finder.cgi) instead of the page.cgi file generally used by the Glinks app. Here is the finder.cgi file:


Code:

use warnings;
use strict;
use CGI::Carp qw(fatalsToBrowser);


use lib '/server/path/to/cgi-bin/subsection/admin';
use lib '/server/path/to/cgi-bin/subsection/admin/lib';


use Geo::Helpers;


my $geo = Helpers->new ('full', '/server/path/to/cgi-bin/subsection/admin/finder.conf');
$geo->Run;



Here is the typical page.cgi code:


Code:

use strict;
use lib '/server/path/to/cgi-bin/subsection/admin';
use Links qw/$PLG/;
use Links::User::Page;


local $SIG{__DIE__} = \&Links::fatal;


Links::init('/server/path/to/cgi-bin/subsection/admin');
Links::init_user();


if ($PLG->dispatch('check_request', \&Links::check_request)) {
$PLG->dispatch('handle_page', \&Links::User::Page::handle);
}



The page that is displayed when I'm using the frontend of finder.cgi is in the template finder.html. If I try to insert any of the globals set in the backend of the Glinks installation, it claims that it is an 'unknown tag'.


Hopefully this provides some clarification.
Quote Reply
Re: [Coert] Code to get globals working in a custom script? In reply to
Hi,

Have you tried something more like:

Code:
#!/usr/local/bin/perl

use strict;
use lib '/path/to/admin';
use Links qw/$IN $DB $CFG $USER/;

use CGI::Carp qw(fatalsToBrowser);

local $SIG{__DIE__} = \&Links::fatal;

Links::init('/path/to/admin');
Links::init_user();


handle();

sub handle {

use Geo::Helpers;
my $geo = Helpers->new ('full', '/server/path/to/cgi-bin/subsection/admin/finder.conf');
$geo->Run;
}

The bits in red are important, as they tell GLinks to correctly init.

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] Code to get globals working in a custom script? In reply to
Hey Andy

Thanks for the code. It didn't seem to make a difference? Same results huh?
Quote Reply
Re: [Coert] Code to get globals working in a custom script? In reply to
Hi,

How exactly are you trying to print the templates out, in:

Code:
my $geo = Helpers->new ('full', '/server/path/to/cgi-bin/subsection/admin/finder.conf');
$geo->Run;

Using GT::Template, or Links::SiteHTML ?

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] Code to get globals working in a custom script? In reply to
Looks like GT::Template

Code:
sub init {
my $self = shift;
my $type = shift;


my $conff = shift;
die "Geo::Helpers->init: config file must exist!" unless ($conff && -e $conff);
$self->{config} = new Config::Simple;
$self->{config}->read($conff) || die "Geo::Helpers->init: error reading config file: " . $self->{config}->error();


$self->{place} = new Place;
$self->{eu} = {'UK' => 1};


$self->{version} = "2.259";


$self->{env} = ( $ENV{'DOCUMENT_ROOT'} =~ /(?:dev\.site\.com)/ ? 'dev' : 'prod' );


$self->log( "init: running in '" . $self->{env} . "' mode\n" );


if ($type eq 'full') {
$self->{q} = new GT::CGI;
$self->{cookie} = {};


# Create a template object using custom settings.
$self->{tf}= new GT::Template({
root => $self->{config}->param('locs.tpl_path'),
compress => 1,
});


Links::init( '/server/path/to/cgi-bin/subsection/admin' );
Links::init_user();
}


$self->{cache} = new Cache (
config => $self->{config},
DB => $DB,
);


my $key = ( $self->{env} eq 'dev'
? $self->{config}->param( 'google.key_dev' )
: $self->{config}->param( 'google.key_prod' ) );


$self->{google} = new Google(config => $self->{config},
tf => $self->{tf},
q => $self->{q},
key => $key
);


}
Quote Reply
Re: [Coert] Code to get globals working in a custom script? In reply to
I seem to remember you have to actually define the globals manually, when using GT::Template

So maybe , when parsing a template with parse() / parse_print(), try also passing this new var into it:

Code:
my %globals = $Links::GLOBALS ? map { $_ => 1 } keys %$Links::GLOBALS : ();

then pass in %globals as one of the params you pass into GT::Template when actually *parsing* the template (not the bit of code you showed above)

Totally stabbing in the dark - as its hard to know without seeing your full code :)

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] Code to get globals working in a custom script? In reply to
This is the parse sub in the for the Geo package:


Code:

sub parse {
my( $self, $tpl, $vars ) = @_;


print $self->cgi->header;


# Don't complain if $vars was undef
$vars = {} if !( defined( $vars ) );
$vars->{'version'} = $self->{version};


eval {$self->{tf}->parse_print( $tpl, { %$vars } )};
if( $@ ) {
$self->log( "$0: error in parse_print(): '$@'\n" );
$self->{tplErr} = $@;
return 0;
} else {
return 1;
}
}







Would this be the correct edit?
Code:

sub parse {
my( $self, $tpl, $vars ) = @_;


print $self->cgi->header;


my $globals = $Links::GLOBALS ? map { $_ => 1 } keys %$Links::GLOBALS : ();


# Don't complain if $vars was undef
$vars = {} if !( defined( $vars ) );
$vars->{'version'} = $self->{version};


$vars->{'globals'} = $globals; #edit


eval {$self->{tf}->parse_print( $tpl, { %$vars } )};
if( $@ ) {
$self->log( "$0: error in parse_print(): '$@'\n" );
$self->{tplErr} = $@;
return 0;
} else {
return 1;
}
}



Quote Reply
Re: [Coert] Code to get globals working in a custom script? In reply to
Yeah, that should work. Again, I'm stabbing in the dark - never had to do it quite this way before (generally I do it using Links::SiteHTML, where the globals are included as standard)

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] Code to get globals working in a custom script? In reply to
Still not working...

How difficult would it be to convert the template handling to Links::SiteHTML? Or should I rather try to work around this?
Quote Reply
Re: [Coert] Code to get globals working in a custom script? In reply to
Hi

Mmm ok.

Where is the path pointing to here?

Code:
root => $self->{config}->param('locs.tpl_path'),

If locs.tpl_path is inside the glinks /admin/templates/folder/ structure, then you could pretty easily use Links::SiteHTML to do it. AFAIK, Links::SiteHTML::display doesn't allow you to pass in a root => xxx value like you can in GT::Template - so you would need it to be in the main template folder (and then link it via a .tplinfo file if you wanted to have it in another folder)

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] Code to get globals working in a custom script? In reply to
Hi Andy. Many thanks for your help so far. I decided to go with a different workaround that avoids the current problem rather than to solve it right now.

Thanks!
Quote Reply
Re: [Coert] Code to get globals working in a custom script? In reply to
Hi,

I might be wrong, but I guess you could use:
http://www.gossamer-threads.com/...ing=%20STASH#p310637

I am very happy with this solution, you can add global values for templates and scripts.

Regards

n||i||k||o

Last edited by:

el noe: Aug 12, 2015, 1:24 AM
Quote Reply
Re: [el noe] Code to get globals working in a custom script? In reply to
Ah, sorry, what I forgot is this part:

Code:
my $GLOBALS = GT::Config->load("$CFG->{admin_root_path}/templates/"
. Links::template_set()
. "/globals.txt", {
inheritance => 1,
compile_subs => 'Links',
local => 1
});

where you can replace the template set Links::template_set() with luna if you only use luna.