Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Alternate Sort Order In Templates

Quote Reply
Alternate Sort Order In Templates
I'm trying to find a way to have a custom template pull sub-categories
from a category, and sort them by the "Description" rather than the "Name"
field. Can this be done? I've got the PageBuilder plug-in at my disposal,
but even with that I can't find a way to use a different sort order. I've
searched the forums, and there's lots of discussion about sort order
generally, but nothing that speaks to using templates...

For example, say it's a list of widgets:

Name => Description
=========================
M423 => Left Side Large Widget
X339 => Bottom Small Widget
Q884 => Left Side Widget Stop
M777 => Upper Oblique Widget

I want to be able to come up with a list sorted by
the Name (Part Code), and another sorted by Description.

Is there a way to do it?

Thanks in advance...
Eric Longman
Quote Reply
Re: [WebWiz] Alternate Sort Order In Templates In reply to
I solved this problem with a global sub:

=========== Global: SortedWidgets ==============
sub {
my $tags = shift;
my $cat_db = $DB->table('Category');
my $condition = GT::SQL::Condition->new(Full_Name => LIKE => 'MainSite/Widgets/%');
$cat_db->select_options ( 'ORDER BY Description');
my $widget_cats = $cat_db->select (['ID','Name','Description'], $condition);
my @output='';
while ( my ($id,$name,$desc) = $widget_cats->fetchrow_array) {
push @output, {id => $id, Name => $name, Description => $desc};
}
return { sortedwidget_loop => \@output };
}

========== Template =========================
<% SortedWidgets %>
<%loop sortedwidget_loop %>
<%Description%> - <%Name%><br>
<%endloop%>


This gets me all categories under the MainSite/Widgets category, and returns them
in a loop sorted by the Description rather than the name. In my template, then,
I can just spin through the loop and show the list the way I want it.

Took some stitching together of examples that I found here on the site, but it works!

Hopefully this'll help somebody else down the road.