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

If Statement against the output from a Global

Quote Reply
If Statement against the output from a Global
Heya all,

My links are listed in categories like this: /Region/Country/State/

I use the global below to split up the category and return either the region, the country or the state for each link. The actual values are not saved in the links table, they're just derived from the category. The example global below returns the country:

Quote:
sub {
my $tags = shift;
my $link_id = $tags->{ID};
my $link_db = $DB->table ('Links');
my ($cat_id, $cat_name) = each %{$link_db->get_categories ($link_id)};
my ($region, $country, $state) = split /\//, $cat_name;
my $output = qq~
$country
~;
return $output;
}

In my templates I'd like to do an "IF" statement to the following effect:

<%if country eg 'United States'%>Do something<%endif%>

This obviously doesn't work as "country" is not a field. Any suggestion how I can get this done?

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] If Statement against the output from a Global In reply to
Hi. I would do it slightly different;

Code:
sub {

my $tags = shift;
my $id = $tags->{ID};

my $cat_tbl = $DB->table('Category');
my $category_full = $cat_tbl->select(['Full_Name'],{ ID => $id })->fetchrow;
return if !$category_full;

my ($region, $country, $state) = split /\//, $cat_name;

return { Country => $country };

}

I would call this with;

Code:
<%-- runs once, and should set the tag--%>
<%global_name%>
<%if Country eq 'United States'%>
... do something here
<%endif%>

This global is untested, so may/may not work right away :)

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] If Statement against the output from a Global In reply to
Andy,

Tried this but it's giving me no output, no error message either. I've made one change to the code (in bold below) and changed the name of the tag to CountrySet:

Global: country_set

Code:
sub {

my $tags = shift;
my $id = $tags->{ID};

my $cat_tbl = $DB->table('Category');
my $category_full = $cat_tbl->select(['Full_Name'],{ ID => $id })->fetchrow;
return if !$category_full;

my ($region, $country, $state) = split /\//, $category_full;

return { CountrySet => $country };

}

Code:
<%country_set%>
<%if CountrySet eq 'United States'%>Something...<%endif%>

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] If Statement against the output from a Global In reply to
Mmm.. it may be easier to change;

Code:
my $tags = shift;
my $id = $tags->{ID};

..with;

Code:
my $id = $_[0];

... and then when calling it;

<%global_name($ID)%>

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!
Post deleted by afinlr In reply to
Quote Reply
Re: [sangiro] If Statement against the output from a Global In reply to
This global works as you have it on Category pages. If you want it on other pages (such as detail pages) then you will need to grab the category id of the link - as you have in your initial global.