Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Custom template - Newbie question

Quote Reply
Custom template - Newbie question
I have searched the forums to try and figure this out, but it isn't working for me. It's a very beginner type question.

I want to create templates to generate pages other than the standard pages that LinksSQL generates (links, categories, etc.). I am basically using the same look on all my pages.

From what I've gleaned from reading multiple forum posts, I should create and save a new template and then use page.cgi to call it.

My questions:

- Does it have to be called using page.cgi, or can it be generated at build time and used as a static page?

- If it has to be dynamic, what is the syntax to call it. I found this in another post: page.cgi?p=customtemplate&ID=<%ID%>&t=customtemplateset

- I think I understand everything except the ID. Where is that picked up from?

If I'm totally out to lunch, please tell me and if possible point me at something I can read that speaks near my level of understanding. Thanks in advance for being patient.

Carl


Quote Reply
Re: Custom template - Newbie question In reply to
Right now, you need to use page.cgi to build all the added-in pages.

The entire format is just page.cig?p=pagename (without the .ext, it defaults to .html, since it goes through the template parser)

(actually, if there is a . in the template name, the template parser should just use it "as is" rather than try to put an .html on the end, but that's a horse of a different color...)

The t= is to use a different template set than default, and that would actually be a good idea, in some ways, since you could put all your non-links pages in a different directory.

(Hey, Alex/GT.... do you see the possiblity for another template directory :) The "user_pages", which page.cgi?p= will default to if it's set in the admin.....)

It wouldn't be too hard to modify nph-build.cgi or create a plugin that will either take the directory referenced above, or a list of templates => output locations, and then parse them and write them out when links is rebuilt.

It actually would be fairly trivial, I think, except the error checking on input would probably outweight the "real" code by a factor of 5-10 : 1.

This would _actually_ be a good feature to build into Links SQL, and while Alex has frozen the features of the next release, this is so trivial, that maybe he'd consider it.

1) Add a user_pages directory option and the associated template storage directory
1a) user_pages => .../template_root/user_picked_name
1b) Used as if $CFG->{'user_pages'} when needed.
2) Output of those pages would be /build_root/user_picked_name
3) Add an option to the nph-build.cgi to "build_user_pages" if $CFG->{'user_pages'}
4) Build all the pages found in the .../template_root/user_picked_name directory and
put them into the /build_root/user_picked_name directory.
5) page.cgi would do the same thing. When a p= request came in, it would check to see if
$CFG->{'user_pages'} was set, and use that directory. Maybe you'd want to make that a
u(ser_templates) or c(ustom) flag, so that the existing p= sites continue to run.


Process finished. This is a generalized process that allow sites to set up an about or info
area. I can additional generalizations, and flags, but that is not the real purpose of Links,
and for the broadest number of users, this simple change would be more than sufficient.

It should be left to a plug-in to generalize further what page.cgi and nph_build.cgi can
do with user-defind templates, and additional template directories.

If Alex/GT doesn't get to it in the 2.1+ release, I'll probably have a module that does
ths in the IMage Galery, to allow providers to customize the look of their site slightly.



PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Plugins:http://LinkSQL.com/plugin
Quote Reply
Re: Custom template - Newbie question In reply to
Thanks very much, pugdog, for taking the time to respond. I finally got the syntax correct and it worked!

Cheers,

cco

Quote Reply
Re: Custom template - Newbie question In reply to
I wrote a really bad script which lets you add in a file in the template directory and then output a static parsed page in an arbitrary folder, which is determined by the prefix specified in the script. I'm reluctant to post it because it's awful. but it's here. It was quite cool when you add in all the gt stuff because you can use 1 template to build 20 sites using <%set whatever tags, and the little module hash things to do values. Anyway, ...



So what you do is:

1) change all the paths
2)add in a new template called whatevertemplateset__index.html
(the prefix being the template set, the suffix being the filename in the directory)
3)add a new cbuild sub specifing the two params
4)name it sdfsfddf.cgi and run it!


#!/usr/local/bin/perl
# ==================================================================
# Links SQL - enhanced directory management system
#
# Website : http://gossamer-threads.com/
# Support : http://gossamer-threads.com/scripts/support/
# CVS Info : 087,070,086,093,090
# Revision : $Id: HACKED UP nph-build.cgi,v 1.52 2001/05/17 03:00:49 alex AND A LAME SUB BY ANDY_C Exp $
#
# Copyright (c) 2001 Gossamer Threads Inc. All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================

# Load Time::HiRes if available for better time checking.
# Must appear here, or we get strange errors.
BEGIN { eval { require Time::HiRes; import Time::HiRes qw/time/; }; }
use lib '/path/to/admin;


#use strict;
use vars qw/$USE_HTML $TIME_START $TOTAL_TIME/;
use Links qw/$DB $IN $CFG/;
use Links::Build qw/compile/;
Links::reset_env() if ($Links::PERSIST);

$| = 1;

local $SIG{__DIE__} = \&Links::fatal;



&c_build("/path/you/want/to/build/in", "prefix for template added in template dir");


# copy that loads of times for each different property , then drop in a template with the right predix


sub c_build {
# ------------------------------------------------------------------
# Generate the added page.
#
$c_build_path = $_[0] ;
$c_set = $_[1] ;



opendir(EXTRA, "/path/to/template dir") || print "poo dearie me messed up";
@c_pages = readdir (EXTRA);
closedir (EXTRA);

if (@c_pages)
{foreach $c_pagename (@c_pages) {if ($c_pagename =~ /$c_set*/){&do;} else {}}}}


sub do{


$c_make = $c_pagename;
$c_feed = $c_pagename;
$c_make =~ s/$c_set//g;
$c_feed =~ s/\.html//g;

my $page = "$c_build_path" . "/" . "$c_make" ;

print "Made $c_pagename \n";


open (ADDED, "> $page") or _cant_open($page, $!);
print ADDED Links::SiteHTML::display("$c_feed");
close ADDED;
my $perms = oct ($CFG->{build_file_per});
chmod ($perms, $page);

}








sub _build_reset_hits {
# ------------------------------------------------------------------
# Updates the What's New flags.
#
_time_start();
print "Resetting hits and rates ... \n";
my $ret = Links::Build::build ('reset_hits', shift || {});
print "Done (", _time_display(), " s)\n\n";
return $ret;
}

sub _build_orphan_check {
# ------------------------------------------------------------------
# Check for orphan links.
#
_time_start();
print "Checking for orphan links ... \n";
my @orphans = Links::Build::build ('orphan_check', { select => ['Title', 'ID'] });
if (@orphans) {
print "\tThere are ", scalar @orphans, " links that are not in a category. Please modify or delete the following links:\n";
my $link_db = $DB->table ('Links');
foreach my $link (@orphans) {
print "\t\t$link->{ID}: $link->{Title}";
if ($USE_HTML) {
print qq~ - <a href="admin.cgi?db=Links&do=modify_search_results&ID=$link->{ID}">modify</a> | <a href="admin.cgi?db=Links&do=delete_search_results&ID=$link->{ID}">delete</a>\n~;
}
}
}
print "Done (", _time_display(), " s)\n\n";
}

sub _build_new_flags {
# ------------------------------------------------------------------
# Updates the What's New flags.
#
_time_start();
print "Updating New Flags ... \n";
my $ret = Links::Build::build ('new_flags', shift || {});
print "Done (", _time_display(), " s)\n\n";
return $ret;
}

sub _build_changed_flags {
# ------------------------------------------------------------------
# Updates the isChanged flags.
#
_time_start();
print "Updating Changed Flags ... \n";
my $ret = Links::Build::build ('changed_flags', shift || {});
print "Done (", _time_display(), " s)\n\n";
return $ret;
}

sub _build_cool_flags {
# ------------------------------------------------------------------
# Updates the What's Cool flags.
#
_time_start();
print "Updating Cool Flags ... \n";
my $ret = Links::Build::build ('cool_flags', shift || {});
print "Done (", _time_display(), " s)\n\n";
return $ret;
}

sub _reset_category_stats {
# ------------------------------------------------------------------
# Reset category stats.
#
_time_start();
print "Checking category stats ... \n";

my $db = $DB->table('Category');
my $cat_link = $DB->table('CatLinks', 'Links');

$db->indexing(0);
my $root_cat = $db->select ( ['ID', 'Full_Name', 'Number_of_Links'], { FatherID => 0 } );
while ( my ($root_id, $root_name, $nol) = $root_cat->fetchrow_array) {
my $children = $db->children ($root_id);
push @$children, $root_id;
my $str = '(' . join(',', @$children) . ')';
my $total = $cat_link->count ( GT::SQL::Condition->new (['CategoryID', 'IN', \$str], ['isValidated', '=', 'Yes']));
if ($total != $nol) {
print "\tCategory $root_name should have $total links, but is set to $nol, repairing ... ";
my $new_nol = _fix_category_stats($db, $cat_link, $root_name, $root_id, $nol);
if ($new_nol != $total) {
print "Structure Error!\n";
_check_category_struc ($db, $cat_link, $root_id, $children);
}
else {
print "$new_nol ok!\n";
}
}
}
$db->indexing(1);
print "Done (", _time_display(), " s)\n\n";
}

sub _check_category_struc {
# ------------------------------------------------------------------
# Find out where the problem is in a category with the wrong link count.
#
my ($db, $cat_link, $root_id, $children) = @_;
foreach my $child_id (@$children) {
my $cat_info = $db->get($child_id, 'HASH', ['ID', 'Full_Name', 'Number_of_Links']);
my $sub_children = $db->children ($child_id);
push @$sub_children, $child_id;
my $str = '(' . join(',', @$sub_children) . ')';
my $total = $cat_link->count ( GT::SQL::Condition->new (['CategoryID', 'IN', \$str], ['isValidated', '=', 'Yes']));
if ($total ne $cat_info->{Number_of_Links}) {
print "\t\t$cat_info->{Full_Name} reported: $cat_info->{Number_of_Links} real: $total\n";
}
}
}

sub _fix_category_stats {
# ------------------------------------------------------------------
# Fix category counts.
#
my ($db, $cat_link, $root_name, $root_id) = @_;
$db->select_options ('ORDER BY Full_Name DESC');
my $sth = $db->select ( GT::SQL::Condition->new ('Full_Name', 'LIKE', $root_name . "/%"), ['ID', 'Full_Name'] );

my %count = ();
my %seen = ();
my $count = $cat_link->count ( { CategoryID => $root_id, isValidated => 'Yes'} );
$count{$root_name} += $count;

while ( my ($id, $name) = $sth->fetchrow_array) {
$seen{$name}++ and print "Duplicate Category Name: ($id) $name\n" and next;
my $count = $cat_link->count ( { CategoryID => $id, isValidated => 'Yes' } );
$count{$name} += $count;
if ($count) {
my @uplevel = split /\//, $name;
for (0 .. $#uplevel - 1) {
my $up_name = join ('/', @uplevel[0 .. $_]);
$count{$up_name} += $count;
}
}
}
while ( my ($name, $count) = each %count) {
my $res = $db->update ( { Number_of_Links => $count }, { Full_Name => $name } );
}
return $count{$root_name};
}

sub _time_start {
# ------------------------------------------------------------------
# Start a timer.
#
$TIME_START = time;
}

sub _time_display {
# ------------------------------------------------------------------
# Return time results.
#
my $end = time;
my $elapsed = sprintf ("%.2f", $end - $TIME_START);
return $elapsed;
}

sub _header {
# ------------------------------------------------------------------
# Print intro.
#
my ($msg, $msg2, $refresh) = @_;
my $time = scalar localtime;

$refresh ||= '';
$TOTAL_TIME = time;
$refresh &&= "<meta http-equiv='Refresh' content='2; URL=$refresh'>";
if ($USE_HTML) {
print $IN->header ( -nph => $CFG->{nph_headers} );
print qq~
<html>
<head>
$refresh
<title>Building HTML Pages</title>
<body bgcolor="white">
~;
print Links::header ("Building HTML Pages: $msg", "$msg2", 0);
print qq~
<pre>Started at $time.

~;
}
else {
print "Started at $time.\n\nBuilding HTML Pages ... \n\n";
}
}

sub _footer {
# ------------------------------------------------------------------
# Print the footer.
#
my $end = time;
my $elapsed = sprintf ("%.2f", $end - $TOTAL_TIME);

print "All done. Total Time: ($elapsed s)\n";
if ($USE_HTML) {
print "</pre></body></html>\n\n";
}
}

sub _build_dir {
# ------------------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.
#
my $input = shift;
my $db = $DB->table('Category');
my $clean_input = $db->as_url ($input);

my ($dir, $path) = '';
my @dirs = split /\//, $clean_input;
foreach $dir (@dirs) {
$path .= "/$dir";
$path = _valid_dir ($CFG->{build_root_path}, $path);

unless (-e "$CFG->{build_root_path}$path") {
print "\tMaking Directory ($CFG->{build_dir_per}): '$CFG->{build_root_path}$path' ...";
if (mkdir ("$CFG->{build_root_path}$path", oct ($CFG->{build_dir_per}))) {
print "Made.";
}
else { print "mkdir failed! Reason: $!."; }
print "\n";
}
}
return "$CFG->{build_root_path}$path";
}

sub _valid_dir {
# ------------------------------------------------------------------
# Only allow a-z A-Z 0-9 / - in a directory.
#
my ($root, $dir) = @_;

if (! -e $root and -d _) {
die "Root directory: $root does not exist!";
}
if ($dir !~ m,^([\w\/\-]+)$,) {
die "Invalid characters in category name: $dir. Must contain only letters, numbers, _, / and -.";
}
return $1;
}

sub _cant_open {
# ------------------------------------------------------------------
# Display a more user-friendly error then Links::fatal.
#
my ($page, $err) = @_;
my $user = eval { getpwuid($>); } || 'webserver';
if ($err =~ /permission/i) {
print "\n\n<b>ERROR:</b> Unable to open '$page' ($!)\n\n";
if (-e $page) {
print <<END_OF_DOC;
This means that the user '$user' is not able to overwrite the
existing file. Please make sure you have set the permissions
in the setup to 0666 if you plan to build from both the web
and shell at the same time.

END_OF_DOC
}
else {
print <<END_OF_DOC;
This means that the user '$user' is not able to create a file
in your pages directory. Please chmod the main directory
0777 so the program can create the file.

END_OF_DOC
}
die "Debug information";
}
else {
die "Unable to open: '$page' ($err)";
}
}
Quote Reply
Re: Custom template - Newbie question In reply to
Pugdog, i was just wondering, but how would you make it as a plugin when there are no hooks on the build process?