Gossamer Forum
Home : Products : Gossamer Links : Discussions :

easy method to add extra templates

Quote Reply
easy method to add extra templates
hello I have post a message about how to add additional templates for user-agreement copyright etc.
this does not work fine, does anybody know if there an easier way and how.
the way pugdog told to me does not work, maybe i'm doing something wrong, but i don't know
help me...

Quote Reply
Re: easy method to add extra templates In reply to
I've added literally dozens of templates.

The way to generate the template is very, very simple.

You have to put that code in the appropriate places though.

If you are using page.cgi, you need to have a test for a page that is
not one of the standard sets, then test to see if the template exists,
if it does, display it, if not generate an error.

But to display a page, all you need to do is:

print $IN->header();
print Links::SiteHTML::display ('template', {hash});

'template' is a file called template.html in the default template directory, and {hash} is a list of passed in values specific to that template.



PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: easy method to add extra templates In reply to
Pugdog,

I'm not a perl expert but really like this add template thing. Could you give a setp-by step instruction here or on your FAQ page. Thanks a lot.

Quote Reply
Re: easy method to add extra templates In reply to
Here is an expanded version of detailed.cgi (which printed detail pages as static pages). This version can accept ID=nnn or Page=template

It might need some debugging, as I didn't run it, but idea is pretty clear.

It first checks for either ID or Page, and doesn't care if both are passed in. If both are passed in, ID takes precedence over Page.

I don't know if this version will run without a fix here or there, since I added the 'page' support to the basic 'ID' support of detailed.cgi

Code:
#!/usr/local/bin/perl
# ==================================================================
# Links SQL - enhanced directory management system
#
# Website : http://gossamer-threads.com/
# Support : http://gossamer-threads.com/scripts/support/
# Revision : $Id: page.cgi,v 1.8 2000/11/09 21:16:57 alex Exp $
#
# Copyright (c) 2000 Gossamer Threads Inc. All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================
#
# This program expects EITHER an ID or a Page passed in.
# ID is checked for a valid ## and existing record, and if ti exists,
# a "detailed" page is printed for that link, as if it were a static page.
# ie: no url translations.
# if no ID is passed in, Page is checked, and if it exists, it's passed
# to the print template routine, which does the existence checking
#
# ==================================================================


use strict;
use lib '/path/to/your/admin';
use Links qw/$IN $DB $CFG $USER/;
use Links::Build;
use Links::SiteHTML;
use vars qw/$LINKDB $CATDB/;

main();

sub main {
# --------------------------------------------------------------
# Wrap in a subroutine to prevent possible mod_perl probs.
#
# Create our database objects.
$LINKDB ||= $DB->table('Links');
$CATDB ||= $DB->table('Category');

if ($IN->param('ID')) {
&generate_detailed_page ()
} else {
&generate_detailed_page()
}
}

sub generate_detailed_page {
# --------------------------------------------------------
# This routine build a single page for every link.
#
my ($page, $id, $link);

$id = $IN->param('ID');
$page = $IN->param('Page');

if (!$id and !$page) {
print $IN->header();
print Links::SiteHTML::display ('error', { error => Links::language('PAGE_INVALIDDETAIL', $id) });
return;
}

($id) && ($link = $LINKDB->get ($id, 'HASH'));

if ($id and !$link) {
print $IN->header();
print Links::SiteHTML::display ('error', { error => Links::language('PAGE_INVALIDDETAIL', $id) } );
return;
}

## need to do some housekeeping, and set up some variables:

my ($cat_id, $cat_name) = %{$LINKDB->get_categories ($link->{'ID'})}; ## Returns the category name & ID from link ID
$link->{'title_linked'} = Links::Build::build ('title_linked', "$cat_name/$link->{Title}");
## Fix up the HTML references
my ($begin, $url, $output); ## setting up for a very, very complicated regex, taken from Links.pm clean_output
$link->{'title_linked'} =~ s!(<a[^>]+href\s*=\s*["']*)$CFG->{build_root_url}/?([^"'>]*)!
($begin, $url) = ($1, $2);
$output = "$1$CFG->{db_cgi_url}/page.cgi?g=" . $IN->escape($2);
$output;
!eisog;

if ($link) {
print $IN->header();
print Links::SiteHTML::display ('detailed', $link);
return;
} elsif ($page) {
print $IN->header();
print Links::SiteHTML::display ($page);
return;
} else {
print $IN->header();
($id) ?
print Links::SiteHTML::display ('error', { error => Links::language('PAGE_INVALIDDETAIL', $id) } ) :
print Links::SiteHTML::display ('error', { error => Links::language('PAGE_INVALIDDETAIL', $page) } );
return;
}
}

1;


PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: easy method to add extra templates In reply to
Thanks a lot, pugdog. Will try to study and test it.Smile

Quote Reply
Re: easy method to add extra templates In reply to
Hi,

You should try and avoid:

$LINKDB ||= $DB->table('Links');
$CATDB ||= $DB->table('Category');

(I know you got this from me). Doing this prevents you to run two separate installations of Links SQL under mod_perl as the tables will get confused.

I've added object caching to GT::SQL, so there is very little overhead to getting a new table object when needed.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: easy method to add extra templates In reply to
Ok,

So what you are saying is generate and release a new object ---

$LINKDB = $DB->table('Links');
$CATDB = $DB->table('Category');

That will create a new $LINKDB and $CATDB in the current block, and
prevent name-space/object-space crashes from previous instances?

I need to understand why I'm doing that :) Not just do it.

Using the ||= was to re-use the object for performance, but you are
saying that in some cases it will cause problems with the various
instances, such as problems that can occur with internal variables?

PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: easy method to add extra templates In reply to
Hi,

Yes, and since you are not caching, they don't need to be global. You would just do:

my $linkdb = $DB->table ('Links');

in the sub you need it.

The reason is consider if you are under mod_perl and have two installations running, one for your FAQ and one for your Postcards. If you do:

$LINKDB ||= $DB->table('Links');

The first time it is run you get a table object for whatever table happened to be called (say it was the FAQ Links table). The next time it's called, the table object exists so you won't get a new table object. However, this time you may be wanting one for the Postcards table in which case you are in trouble!

Does that make sense?

Actually, this isn't an issue if you cache inside a CGI script, but it is if it's inside of a shared library (like a plugin file, or something else).

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: easy method to add extra templates In reply to
ok I think too difficult, so only page.cgi should be modified.
But How can i call this template from my browser, I mean:
http://yourhost.com/cgi-bin/page.cgi and wat next?????
Maybe A stupid question but i really don't know.



Quote Reply
Re: easy method to add extra templates In reply to
You would add this sub to page.cgi and just make the changes Alex suggested -- make a local my $new_db_object rather than reuse $DB

You'd have this sub handle anything that wasn't of the already existing kinds, before generating an error.

PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: easy method to add extra templates In reply to
Hi,

In the next beta (beta 3) you will be able to call:

page.cgi?p=template

and it will load the template for you.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: easy method to add extra templates In reply to
Sorry if this is a dumb question. Could it be somthing like
page.cgi?p=productall_template&vender_name=GT
to show a, say great web building products of GT and
page.cgi?p=product_template&vender_name=GT&product=LQ2
to show detail description of this great product?Smile



Quote Reply
Re: easy method to add extra templates In reply to
Hi,

You pass in p=something and it will load that template. From there, you can use anything else that's passed in as you see fit. For example:

page.cgi?p=list_reviews&author=alex

Then in your list_reviews.html template:

View all Reviews owned by <%author%>

and you can write custom functions that take the cgi input and do whatever they like with it.

Cheers,

Alex

--
Gossamer Threads Inc.