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

Pass var to global

(Page 1 of 2)
> >
Quote Reply
Pass var to global
Imagine three countries:

germany
italy
spain

with some cities

on one page i want to show:

germany
all subcats

italy
all subcats

spain
all subcats


I have no problem to do this, but i do it with three globals like

...
all cats with fatherID = 'ID of cat italy
...


It is no big story, but maybe someday i want to have 50 countries, then it would be really nice to say in the template something like:

<%list_of_subcats(n)%>

while n is the FatherID.


I have no idea how to do this and hope for help.
Quote Reply
Re: [Robert] Pass var to global In reply to
Now i remember, normaly i do this with a global for all my countries and cities and a field for sorting,
but if i want to have some design things between the countries it would be better to use one global for every country and much better to have only one global and pass the fatherid to it.

It seems Andy has made something as a plugin here

http://www.gossamer-threads.com/forum/Products_C9/Gossamer_Links_C5/Development%2C_Plugins_and_Globals_F20/Display_all_Categories%2C_SubCategories%2C_and_their_Links_on_homepage_P316350/?page=unread#unread


Hmm, i never worked with plugins, but maybe i can pass the id also to a global. I just will try it now ...
Quote Reply
Re: [Robert] Pass var to global In reply to
Hi,

So you just want to pass a variable from the tag, into the global? Its pretty simple:
Code:
<%foo(1234)%>

sub foo {
my $first_variable = $_[0]; # specifically get the first value passed in

# ...or:
my @variables = @_; # put all the passed in values into an arrya

# or


my $foo = shift; # get the first variable passed in

}

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] Pass var to global In reply to
Thank you very much. I still love this old lady here. :)
Quote Reply
Re: [Robert] Pass var to global In reply to
I use <%a_global(1)%> in my template;
and my $x = $_[0]; to have the value "1".
Ok.

In another global i use:

my $tags = shift;

if ($tags->{CategoryID}) {
$catid = $tags->{CategoryID};
} else {
$catid = $tags->{ID};
}


To have another value from the call (i use different templates for different languages) i tried this:

<%set a=1%><%a_global%>

Then i use:

my $tags = shift;

if ($tags->{CategoryID}) {
$catid = $tags->{CategoryID};
} else {
$catid = $tags->{ID};
}
my x = $tags->{a};

And everything is ok.
Quote Reply
Re: [Robert] Pass var to global In reply to
Hi,

This code:

Code:
if ($tags->{CategoryID}) {
$catid = $tags->{CategoryID};
} else {
$catid = $tags->{ID};
}

Could be trimmed to just:

Code:
my $catid = defined $tags->{CategoryID} ? $tags->{CategoryID} : $tags->{ID};

Cool

For the other bit - are you asking a question? I can't work out if you are just saying how you have done it already, or if you need some more help Angelic

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] Pass var to global In reply to
Everything ok.
If i need a second var, i pass a number like digits (or make an array with set?)
Quote Reply
Re: [Robert] Pass var to global In reply to
Hi,

Personally, instead of doing lots of "sets" , you can simply pass the values in:

Code:
<%global_name("Foo","bar",$ID)%>

sub {
my ($var1,$var2,$ID) = @_;
#...
}

You then would have no need for the $tags line, as you are only passing in the variables you want

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] Pass var to global In reply to
That is more nice than my version of "var1,var2,var3" and a split inside the global.
Thank you very much.

Now i try the opposite way.
Code:

For example, assume a variable named ``person'' has been passed to the template parser with a value of:
{ name => "John Doe", age => 35, hair => "brown" }
The following example:
<%person.name%> is <%person.age%> and has <%person.hair%> hair.



Because of different reasons i have to pass all my text-fields to utf: (I know that i could change the setting inside lsql)

global utf_Title
Code:
sub {
my $tags = shift;
my $tmp= $tags->{Title};
utf8::encode($tmp);
return $tmp;
}

i would like to have:

global utf_all
Code:
sub {
my $tags = shift;
foreach $tags as $key=>$value {
utf8::encode($tags[$key]);
}
return @tags
}

Then in template i use <%tags.title%>

I will give it a try now.

Maybe it would be better to make the utf-translation directly in the script and just use the normal <%field%>?

Last edited by:

Robert: May 11, 2016, 4:29 AM
Quote Reply
Re: [Robert] Pass var to global In reply to
Code:
sub {

my $values = {

name => "John",
age => 25,
hair => "Brown"
};

return { person_values => $values }

}

Then call with:

Code:
<%global_name%>

<%DUMP person_values%>

...i.e:

Code:
<%person_values.name%>

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] Pass var to global In reply to
You are faster than light!
I just have edited my text above and have your answer before finishing my edit. :)
Quote Reply
Re: [Robert] Pass var to global In reply to
haha

With the loop, you want to be careful when encoding (as you can end up double encoding if you are not careful). Anyway, this should do the trick:

Code:
my $tags = shift;
map {
$tags->{$_} = utf8::encode($tags->{$_})
} keys %$tags;

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] Pass var to global In reply to
Thank you.
But if i use a global i need time to calculate it.
Anyway the script passes all my fields. So it would be better to encode directly in the script instead, isnt it?
I found in SQL/tables.pm the function get:

Code:
$COMPILE{get} = __LINE__ . <<'END_OF_SUB';
sub get {
# -----------------------------------------------------------
# get()
# IN : primary key and format options, and fields wanted.
# OUT: array_ref/hash_ref on success, undef on failure.
#
my $self = shift;

# Connect to the database if we are not already connected
$self->connect or return;

my (@keys, @pk, @sel, $cond, $method, $format, $cols);
$self->name or return $self->fatal('NOTABLE');
$cond = GT::SQL::Condition->new;

if (@_ == 0) { return $self->fatal(BADARGS => 'Usage: $obj->get(HASH or HASH_REF or CGI_OBJ)') }
elsif (ref $_[0] eq 'HASH') {
my $href = shift;
for (keys %{$href}) {
$cond->add($_, '=', $href->{$_});
}
}
else {
@keys = ref $_[0] eq 'ARRAY' ? @{shift()} : (shift);
@pk = @{$self->{schema}->{pk}};
while (@keys) {
$cond->add(shift(@pk), '=', shift(@keys));
}
}

$format = uc shift || 'HASH';
$cols = shift || [];
$method = $format eq 'ARRAY' ? 'fetchrow_arrayref' : 'fetchrow_hashref';
my $sth = $self->select($cond, $cols);
if ($sth) {
return $sth->$method();
}
else {
return;
}
}
END_OF_SUB

But i am not shure if any select is made with this function, so maybe i find the right place in build.pm better.
For detailed e.g. here:

Code:
my $link_row = $link_db->get($id, 'HASH');
@$link{keys %$link_row} = values %$link_row;

and after i add something like:

Code:
foreach @link as $key=> $value {
utf8::encode($link[$key]);
}
Quote Reply
Re: [Robert] Pass var to global In reply to
Hi,

A select() is made from a get() command (its a wrapper) ... however, I would be VERY careful when doing utf8 conversions in there... as it will have issues across the whole system (not just the pages being built).

I'm still not quite sure why you want/need to do the UTF8 conversion. With the correct <meta> header for utf8,. and charset in Setup > Build Options, you shouldn't need to worry about converting anything.

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: [Robert] Pass var to global In reply to
Finally ... after having really a lot of fear to change my database, tables, values i tried it a minute ago.
Now i have the database in utf, the tables in utf, lsql in utf and ...

... a lot of problems.

I watch:

1.phpMyAdmin
2.My own script (coded as utf, with <meta charset="utf-8" />
3. lsql admin
4. the pages


When i retype München/Munich in phpMyAdmin i have it correct in my script (2.)
but not in lsql admin.

When i retype München/Munich in lsqql admin i have it correct there but not in phpMyAdmin.

So it seems there ist anything wrong here, but where?

The html in lsql shows <meta charset="utf-8" />. Maybe there is anything wrong inside the script itself with the encoding?
Quote Reply
Re: [Robert] Pass var to global In reply to
I have retyped my cats with äöü in lsql, now i have it wrong in phpMyAdmin , but correct on my builded pages.
Oh heaven!
Quote Reply
Re: [Robert] Pass var to global In reply to
Ok.
I saved all the admin templates with utf-encoding and found this here

$options = array
(
/*PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',*/
);

in my script to change my Links; without the encoding is now everywhere ok.
Quote Reply
Re: [Robert] Pass var to global In reply to
Some time ago we have spoken about this:
http://www.gossamer-threads.com/forum/Products_C9/Links_2.0_C2/Customization_F4/UTF8_-_SET_NAMES_for_mysql_P315479/

Now i have set everything to UTF, but cant use the lsql admin anymore.
There should be really a place in the script where i can incode or decode, isnt it?
Quote Reply
Re: [Robert] Pass var to global In reply to
Ok.
I hope someone understands it and could explain it to me, please.

I have saved all .html of lsql with encoding UTF8, i have inserted everywhere

<meta charset="utf-8" />

also two times in Links/admin.pm and SQL/admin.pm

Means that every page lsql shows has the utf8 set.
The database and all tables are set to utf8

My script (written in php to change data in LSQL) is set to utf8

In LSQL, in my script and in my builded pages i have the correct "München for munich written.

But if i watch my data in phpMyAdmin (database and tables and text- fields are set to utf8)
i see strange values. The database connection is set to uft8 also.

So this means what?
The data is saved as something else, but LSQL is able to translate it right, my php is able to translate it right?
Then i need only to tell phpMyAdmin to do this also? (I tried with changiung the connection, i tried to change the encoding in Firefox also, but it fails)

What the hell is saved in MYSQL?

:)
Quote Reply
Re: [Robert] Pass var to global In reply to
It seems that i finally have everyting to UTF8, but not SETUP > MISC OPTIONS > header_charset.
Here i have to stay with Western 8859-1 to see everything in the right way.
Another lost day because of this charset-story. :)

Last edited by:

Robert: May 11, 2016, 2:44 PM
Quote Reply
Re: [Robert] Pass var to global In reply to
Don't even get me started on charset issues ... I've lost days with it as well ;) (especially when working on older sites)

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] Pass var to global In reply to
Andy wrote:
Code:
sub {

my $values = {

name => "John",
age => 25,
hair => "Brown"
};

return { person_values => $values }

}


Then call with:

Code:
<%global_name%>

<%DUMP person_values%>


...i.e:

Code:
<%person_values.name%>


Cheers


I am sitting in front of a forrest and cant see any trees. :)

I tried out, but nothing happens, the global is not there.

name of global: test

return { person_values => $values }

now i need in my template?

<%test.person_values.name%>
...
...
...
Ok. Got it.

Last edited by:

Robert: May 26, 2016, 12:33 PM
Quote Reply
Re: [Robert] Pass var to global In reply to
It seems i can use something like

test.test1.a

only after making a dump.

Last edited by:

Robert: May 26, 2016, 12:48 PM
Quote Reply
Re: [Robert] Pass var to global In reply to
global test:

sub {
return { age => "15", color => "red" };
}

<%test.age%> is not defined!

Only after a dump, i can see it.
So i need to make a dump inside a <!-- -->

Seems strange.


.... OK ... you have written the answer before:

<%test%>
<%test.age%>

would do it. :)

Last edited by:

Robert: May 26, 2016, 1:15 PM
Quote Reply
Re: [Robert] Pass var to global In reply to
Hi,

You shouldn't need to do a <%DUMP ..%> to make it work :) What exactly is the template + global you are using?

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!
> >