Gossamer Forum
Home : Products : Gossamer Links : Discussions :

I Need Parents

Quote Reply
I Need Parents
I have created a template global called get_parents:
Code:

sub { # Get the parents of an ID
my $category_id = shift;
my $category = $DB->table('Category');
my $parents = $category->parents ( $category_id );
return $parents;
}

In the template I call this sub-routine as <%set data=get_parents(84)%>

A dump of the returned data is: (why the backslash '\' ?)

\[
'3',
'396',
'64'
]

The parent id numbers being returned are correct. But, I can't access them in the template. I have tried:

<% data %>
<% $data %>
<% data[0] %>
<% $data[0] %>
<% @data[0] %>
<% @$data[0] %>

Please tell me what I'm doing wrong.

Thank you,
Roger
______________________________
Roger "Teresk" Brown
Stratics Central Content Director
Stratics Forums Programmer
Guild Forums Administrator
teresk@stratics.com
Quote Reply
Re: [Teresk] I Need Parents In reply to
With the current GT::Template, you can't do that set. Instead, you'll need to:
Code:
return { varname => $parents };
and in the code, you can do <%varname.0%>

Adrian
Quote Reply
Re: [brewt] I Need Parents In reply to
Thank you, brewt! I now have it working properly!!

Just a side note for others that might want to use this code:

Where I said "In the template I call this sub-routine as <%set data=get_parents(84)%>"... that needs to change to "<%get_parents(84)%>". The assignment to "data" is now being made in the template global.

The template global is now:

sub { # Get the parents of an ID
my $category_id = shift;
my $category = $DB->table('Category');
my $parents = $category->parents ( $category_id );
return { data => $parents };
}

The dump of the data:

[
'3',
'396',
'64'
];


And the template is "<%get_parents(84)%>parents: 0:<%data.0%>, 1:<%data.1%>, 2:<%data.2%>"

Which now displays: "parents: 0:3, 1:396, 2:64".

Thanks again, brewt!

Roger
______________________________
Roger "Teresk" Brown
Stratics Central Content Director
Stratics Forums Programmer
Guild Forums Administrator
teresk@stratics.com

Last edited by:

Teresk: Jul 5, 2006, 4:41 PM