Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

Template2 - Different Category Templates Mod

Quote Reply
Template2 - Different Category Templates Mod
As my first contribution :-), these are very easy instructions to simply modify LinksSQL to use a different template for each category.

1) You need to add a field to the Category table (Table Maintenance section in the Links Manager). I called it Template2. Make sure you set the default value field to category.html.

2) Open nph-build.cgi (in admin directory) and in # Get the category info., add the following line:
Code:
$OUT{template2} = $category_r->{'Template2'};
So that it ends up reading:
Code:
# Get the category info.
%OUT = (); # Start with a clean hash.
$OUT{category_id} = $category_r->{'ID'};
$OUT{category_name} = $category_r->{'Name'};
$OUT{header} = $category_r->{'Header'};
$OUT{footer} = $category_r->{'Footer'};
$OUT{meta_name} = $category_r->{'Meta_Description'};
$OUT{meta_keywords} = $category_r->{'Meta_Keywords'};
$OUT{description} = $category_r->{'Description'};
$OUT{template2} = $category_r->{'Template2'};
$OUT{random} = rand (10000);
$OUT{random1} = rand (10000);
$OUT{random2} = rand (10000);
$OUT{random3} = rand (10000);
3) Open HTML_Templates.pm (in admin/Links directory) and in sub site_html_category, replace this:
Code:
my $output = &load_template ( 'category.html', {
For this:
Code:
my $output = &load_template ( $tags->{'Template2'}, {
After all this is done, simply make copies of category.html in your templates folder and name them according to each category and modify as you wish.
Be sure to set the contents of the field "Template2" when creating or modifying a category to the name of the template (ie. instead of category.html, category_name1.html or whatever you named it).

Good luck!


Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
Simple and effective... for clarity, though, I'd suggest changing the "Template2" variable to something more meaningful, such as "Custom_Template" or "Local_Template".

By using the field for _all_ calls, either with the default value, or with a custom value, you get rid of needing to test for value, and increase performance at the cost of some space.

For a "safety" net, in case someone does something silly, you can even test to make sure the Custom_Category_Template.html files exist, although that will be done for you by the error trapping mechanism of Template.pm, but you can bypass a lot of potential errors, by changing the $OUT{Template2} assignment:

($category_r->{'Template2'}) ? $OUT{Template2} = $category_r->{'Template2'} : $OUT{template2} = 'category.html';

Which makes sure that there is a value in the Template2 field of the Category record (and that it wasn't deleted or erased by mistake) and sets the default category template if it was. If you are careful, this is a redundant test.

You could accomplish the same thing by adding a line above the $OUT{Template2} :

(!$category_r->{'Template2'}) && ($category_r->{'Template2'} = 'category.html');

Which will assign a default value to $category_r->{'Template2'} if the field is empty, or ignore it if it's not. (The only way the RIGHT side of an "AND" expression is evaluated at all, is if the LEFT side is TRUE -- in this case, empty.).

You can also get tricky, and make the "if" test a regex, checking for a /pattern.html/ type format in the Template2 field.

I'm not sure what the performance hit on this would be, (which of the 3 options is better speed/resource wise) but there are these 3 options for doing this.

<G> as you can see, I've thought about this alot <G>


http://www.postcards.com
FAQ: http://www.postcards.com/FAQ/LinkSQL/

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
Hmm... impressive :-) but I´m just happy it´s working :-)
I´ll keep your comments in mind though.

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
I would like to use use the high level category name for my template name. Can some one give me the perl to parse the data string unlinked_title to get the value?? Thanks.

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
I had thought about this... basically you'd want a category to default to lowest-level custom template above it.

For instance, if you have 10 categories on the main table, for ease of thought, we'll call them 1-10, you'd want every addition to every subcategory in the tree below the #1 category for example, to use the #1 custom template, or use the default if none was defined.

If you had a category #1-#1.1-#1.1.2, you'd want #1.1.2 to first check _it's_ parent node, #1.1 for a custom template, and if it finds one, use that as the 'default'. If it doesn't, it works it's way up the tree to the #1 category, and if that has a custom template uses that, if not use default.

For instance, if you had a category Computers, and three subcategories :Mac,PC,Other, you'd want to assign a default "Computers.html" template to the category Computers. Then you'd want to assign a default template Computers_Mac.html to the Mac topic, and Computers_PC.html to the PC category. Other, would default to the default Computers.html template. If you added a category Mac/iBook, the iBook would _default_ to the Computers_Mac.html template, since that was the closest (lowest level assignment) to it.

Make sense?

This will be easier in the next release, since you'll be able to know who is the parent.

To get to your specific question, you'd want to use a pattern to grab the top level category out of the Title of the category you are entering (up the first \)(you could use split, as a dirty quick hack), then use that to do an exact match on the Category table, get the record that matches, (the top level category) and assign the Custom_Template of the new record, to be the same as the Custom_Template of the record you just searched for.

http://www.postcards.com
FAQ: http://www.postcards.com/FAQ/LinkSQL/

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
This is a nice solution and easily solved my problem.

Is there a easy solution to add a different link template to each category???
I really only need 2 link.html files which would be based on the category template. I've read through the other posts on multiple categories, and cannot really follow it.

I know in the category.html it calls the links section with <%links%> would it be possible to create another ie. <%links_2%>, that would use another link.html template.


I have read through all the posts in the forum and FAQ.
Any other suggestions would be appreciated.

RWhite

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
It is possible by using one of the following methods:

1) Hack sub site_html_link in HTML_Templates.pm

Code:

if ($rec->{'CategoryID'} eq '19') {
$template = "link2.html";
}
else {
$template = "link.html";
}


Then replace link.html with $template in the following codes:

Code:

my $output = &load_template ('link.html', {


Like the following:

Code:

my $output = &load_template ($template, {


Then create another template file called link2.html.

NOW, if you want to check the CategoryID field with the actual category name, then you will have to use the following codes:

Code:

my $catname = &Links::DB_Utils::get_category_name($rec->{CategoryID});
if ($catname eq 'Something') {
$template = "link2.html";
}
else {
$template = "link.html";
}


2) Hack the sub build_category_pages in the nph-build.cgi file

Replace the following codes:

Code:

$OUT{links} .= &site_html_link ($tmp);


with the following codes:

Code:

if ($OUT{category_name} eq 'Something') {
$OUT{links2} .= &site_html_link2 ($tmp);
}
else {
$OUT{links} .= &site_html_link ($tmp);
}


Note: There are a bunch of these codes in the subroutine, so you need to edit all of them.

You will also need to create another subroutine in the HTML_Templates.pm file called sub site_html_link2 and this subroutine should load link2.html.

Then in your category.html template file, you need to add the following codes:

Code:

<%if links2%>
<%links2%>
<%endif%>


Hope these suggestions help.

Regards,

Eliot Lee

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
Eliot,
Thanks for your reply.
I tried the first suggestion and received a error:Undefined subroutine &Links::HTML_Templates::load_links_template called at Links/HTML_Templates.pm line

Does this look right?
# Figure out how many days old it is if it's a new link.
if ($LINKS{build_days_old}) {
if ($rec->{'isNew'}) {
$rec->{'Days_Old'} = &Links::DBSQL::date_diff (&Links::DBSQL::get_date(), $rec->{'Add_Date'});
}
else { $rec->{'Days_Old'} = ''; }
}
my $user = {};
defined $dynamic and &load_user ($dynamic, $user, $rec->{CategoryID});
if ($rec->{'CategoryID'} eq '19') {
$template = "link2.html";
}
else {
$template = "link.html";
}
my $output = &load_links_template ($template, {
detailed_url => "$LINKS{build_detail_url}/$rec->{'ID'}$LINKS{build_extension}",
%$rec,
%$user,
%GLOBALS
}, undef, $template);
# Don't clean output on a single link.
return $output;
}


Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
Actually...you need to use &load_template...I forgot to replace my custom subroutine call with the original one.

It should work now.

Regards,

Eliot Lee

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
Eliot,
Excellent, works like a charm. Thank You!!

RWhite

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
You're welcome.

Regards,

Eliot Lee

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
I've been through this 10 times this morning and end up with blank pages no matter what I do.

I created Template2 in category.db, resync .def, it looks like;
Template2 =>['14', 'CHAR', '40', '40', '0', 'category.html', '', '0']

Changed nph_build to;
# Get the category info.
%OUT = (); # Start with a clean hash.
$OUT{category_id} = $category_r->{'ID'};
$OUT{category_name} = $category_r->{'Name'};
$OUT{header} = $category_r->{'Header'};
$OUT{footer} = $category_r->{'Footer'};
$OUT{meta_name} = $category_r->{'Meta_Description'};
$OUT{meta_keywords} = $category_r->{'Meta_Keywords'};
$OUT{description} = $category_r->{'Description'};
$OUT{description2} = $category_r->{'Description2'};
$OUT{number_of_graphics} = $category_r->{'Number_of_Graphics'};
$OUT{template2} = $category_r->{'Template2'};
$OUT{random} = rand (10000);
$OUT{random1} = rand (10000);
$OUT{random2} = rand (10000);
$OUT{random3} = rand (10000);


Changed sub site_html_category to;
sub site_html_category {
# --------------------------------------------------------
# This rountine will build a page based for the current category.
#
my ($tags, $dynamic) = @_;
my $template = defined $dynamic ? $dynamic->param('t') : undef;
(ref $tags eq 'HASH') or croak "HTML_TEMPLATES: Argument '$tags' must be hash reference";

my ($name) = $tags->{'category_name'} =~ m,/?([^/]+)$,;
defined $dynamic and &load_user ($dynamic, $tags);
my $output = &load_template ( $tags->{'Template2'}, {
%$tags,
build_links_per_page => $LINKS{build_links_per_page},
category_first => $name,
%GLOBALS
}, undef, $template );
defined $dynamic and &clean_output($dynamic, \$output);
return $output;
}


Just to test I filled Template2 for all categories to category.html. I've removed the above changes and pages build fine. What am I missing here??

Thanks,
RWhite


Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
I think you are missing this:

$OUT{template2} = $category_r->{'Template2'};


'template2' vs 'Template2'



http://www.postcards.com
FAQ: http://www.postcards.com/FAQ/LinkSQL/

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
pugdog,
Thanks! That was it.

R White

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
Hello to all.
Emm, wel i have the same problem but i tra to chance
the
$OUT{template2} = $category_r->{'Template2'};
to $OUT{Template2} = $category_r->{'Template2'};
but still come the page on white.
i am using dinamic pages, i hope this not be the problem.


Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
yes, that's the problem.

You need to make the same/similar change to the routine in page.cgi.

Page.cgi does not use nph-build.cgi, so anything you do to nph-build.cgi has to be done to page.cgi in order for the dynamic pages to display the same as the static ones.

http://www.postcards.com
FAQ: http://www.postcards.com/FAQ/LinkSQL/

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
Ok, i make the same chances in de page.cgi and WORK´S
the ploblem ist the page.cgi use the nphbuil.cgi for
load all the templates.

Other one small cuestión.
when usin the page.cgi, if i have in the template like
, this dont work,but if i´am using the not dinamyc mode, this work
perfect.
you know way???

Thanks!, Thanks!



Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
FROM THE LAS post about the include
<<--#include virtual="/cgi-bin/pub/header2.html"-->>

Quote Reply
Re: Template2 - Different Category Templates Mod In reply to
No,

The page.cgi does not call nph_build.cgi at all.

It does call HTML_Templates.pm for template processing, but it doesn't seem to call &load_template directly at all.

You need to make the changes to page.cgi in each of the routines that use any sort of modified templates used by nph_build.cgi.


http://www.postcards.com
FAQ: http://www.postcards.com/FAQ/LinkSQL/