Gossamer Forum
Home : Products : DBMan SQL : Discussion :

Using templates to generate "Static Pages"

(Page 1 of 2)
> >
Quote Reply
Using templates to generate "Static Pages"
I am using DBSQL 2.05 and all is well as far as displaying information from the database. What I've also been trying to do is use the DBSQL functionality to generate my "Static Pages" from the template parser ie: my FAQ page.

I've created an faq.html in my template directory with all my FAQ Q & A contained therein with standard html. What I am having difficulty with is generating the proper url to display this page.

For instance in my home.html template I've added this tag :
Code:
<a href="db.cgi?do=faq&db=<%db%>">FAQ</a>
Which when clicked on gives me an "unknown action" error.

What codes/tags do I need to use to properly generate a url to just display a page/template? Many thanks in advance for any assistance.

easy does it
Quote Reply
Re: [Bearwithme] Using templates to generate "Static Pages" In reply to
Hi,
just simply add the code below into Dbsql/Home.pm:

$COMPILE{faq} = <<'END_OF_SUB';
sub faq {
my $self = shift;
# your code goes here, and return to template if you want.
$self->print ('faq.html',{});
}
END_OF_SUB

hope that will help,
doublemint
Quote Reply
Re: [doublemint] Using templates to generate "Static Pages" In reply to
Excellentamundo! That works...Am I to understand that for whichever "static pages" I wish to display that I will have to add a similar sub for each page? It seems like it would be more reasonable to be able to pass a page variable in the URL for any that aren't pre-defined in Home.pm and have it done on-the-fly? (or am I missing the big picture - most likely).

Also wherever I use this structure in my templates :
Code:
<a href="db.cgi?<%ifnot use_cookie%>sid=<%session_id%>&<%endif%>do=some_page&db=<%db%>">

I get this in the resultant template display :
Code:
db.cgi?sid=Unknown Tag: 'session_id'&do=some_page&db=Sample

Not sure why that is, as session_id is defined in my Sample_user_sessions table and is generating a session_id in the table.

Thank you for your help.


easy does it
Quote Reply
Re: [Bearwithme] Using templates to generate "Static Pages" In reply to
yeah, definitely, you could do something like that, just change to the following code:

......
my $file = $self->{cgi}->{page} || 'faq.html' ; #default to faq.html
#check if user passed on some tricks in filename
unless ($file =~ m,^([-\w. ]+)$, and $file !~ m,\.\.,) {
return $self->error("Invalid page $file");
}

$self->print ($file,{});
......

into faq sub-routine then in your templates:

<a href="db.cgi?<%ifnot use_cookie%>sid=<%session_id%>&<%endif%>do=faq&page=some_pages&db=<%db%>">
Quote:
db.cgi?sid=Unknown Tag: 'session_id'&do=some_page&db=Sample
this error happens when some hidden tag session_id ,in some templates, which doesn't have a valid <%if session_id%>...<%endif%>

cheers,
doublemint
Quote Reply
Re: [doublemint] Using templates to generate "Static Pages" In reply to
You're golden man! It works great...actually I changed sub faq to sub page so now I can generate urls to any static page via template by using:
Code:
<a href="db.cgi?do=page&page=some_page.html&db=<%db%>">

I'm still having trouble with the <%session_id%> tag. I can't seem to get it to generate one properly for inclusion in a url ie:
Code:
<a href="db.cgi?do=page&page=some_page.html&db=<%db%>&sid=<%session_id%>">

What I'm driving at is having a totally template driven site
via db.cgi and maintaining state throughout site with session_id.

I've got DBSQL running from my public_docs directory (index.cgi is actually a copy of db.cgi) so when someone hits my domain home page it is actually DBSQL generating all the pages - static or otherwise. That part is working great but I'm having trouble generating a session_id when a visitor comes in for that initial hit.

Does that make any sense? Thank you greatly for your help!


easy does it
Quote Reply
Re: [Bearwithme] Using templates to generate "Static Pages" In reply to
Hi,

thats a very usefull mod. However, since we all start over with dbman sql 2.0 and know how hard it is to maintain mods over several changes and releases...

Shouldn't that be a plugin? I would like to see, how such changes would be implemented as plugins.

Thanks, Henning
Quote Reply
Re: [ktt] Using templates to generate "Static Pages" In reply to
Quote:
thats a very usefull mod. However, since we all start over with dbman sql 2.0 and know how hard it is to maintain mods over several changes and releases...

Shouldn't that be a plugin? I would like to see, how such changes would be implemented as plugins.

Actually the code changes at this point comprise only a few lines of code as provided by doublemint and would not warrant a plug-in architecture but rather most likely incorporated as a feature in the next release. But of course that is entirely up to the GT programming team
Smile
easy does it
Quote Reply
Re: [ktt] Using templates to generate "Static Pages" In reply to
yeah, it's really easy for you to create a plugin like that, find attached the static_page plugin for you to try out (version 2.0.1). Just install it and you can make a link like below:
<a href="db.cgi?do=home&page=some_page.html&db=<%db%>">some_page.html</a>

cheers,
doublemint
Quote Reply
Re: [doublemint] Using templates to generate "Static Pages" In reply to
opss, I don't know where the hell my attachment have gone when I upload the file...Crazybut if anyone is interested in them just leave me a pm,Wink
Regards,
doublemint

Last edited by:

doublemint: Oct 30, 2001, 5:01 PM
Quote Reply
Re: [doublemint] Using templates to generate "Static Pages" In reply to
Hi,

If you want to email it to me, I can put it up on Gossamer so that others can download it from their admin.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [doublemint] Using templates to generate "Static Pages" In reply to
Cool. Simple but works. Keeps the code clean. Must be the first dbman plugin!
Quote Reply
Re: [Bearwithme] Using templates to generate "Static Pages" In reply to
In Reply To:
I'm still having trouble with the <%session_id%> tag. I can't seem to get it to generate one properly for inclusion in a url ie:

<a href="db.cgi?do=page&page=some_page.html&db=<%db%>&sid=<%session_id%>">

Could you find out the attached file. That should solve your prolem.

Cheers,
TheStone.

B.
Quote Reply
Re: [doublemint] Using templates to generate "Static Pages" In reply to
Hi,

the plugin worked fine in 2.0.0 but is not working as fine in Version 2.0.2. This is preventing easy upgrades. Frown

First Issue: File System structure has changed

In Version 2.0.2 plugins are in admin/Plugins/DBsql (before it was admin/Plugins). When you upgrade an existing installation the Plugin Manager is not any longer finding the Plugins already installed.
Workaround:
1. You need to reinstall the distributed tar file.
2. You need to insert ::DBsql in any function call (e.g. hook install and uninstall sections etc.) of the Plugin Files.
3. If applicable you need to change your functions calls in your templates and add ::DBsql

Second Issue:
I found it easier to rebuild the plugin using the wizzard.Unsure But it has at least one bug with the new file system structure. The install.pm is inserting wrong hook calls (the ::DBsql is missing).
Workaround: patch those calls to $mgr->install_hooks and $mgr->uninstall_hook by inserting the missing ::DBsql


Third Issue:
After Processing the plugin DBsql ist sarching a template named "1". I don't know where that Problem is located. It might be the Plugin itself, because the GT XML sample plugin does not have this problem.
Workaround: Provide an empty file name "1" in the templates directory.

For reference here is the plugin code. It is hooked to home. So you can call it with db.cgi?do=home&page=the/page/to/show.html&db=yourdb. The html file needs to be in the template directory.
Code:
sub page {
# -------------------------------------------------------------------
# This subroutine will get called whenever the hook 'home'
# is run. You should call GT::Plugins->action ( STOP ) if you don't
# want the regular code to run, otherwise the code will continue as
# normal.
#
my ($args) = @_;

# Do something useful here
my $file = $args->{cgi}->{page} || '' ;
$file or return $args;
#check if user passed on some tricks in filename
unless ($file =~ m,^([-\w. \/]+)$, and $file !~ m,\.\.,) {
return $args->error("Invalid page $file");
}
# you can add some vars and return to the template here, good luck
$args->print ($file,{});
GT::Plugins->action ( STOP );
}

Any Help would be appriciated!

Thanks


Quote Reply
Re: [ktt] Using templates to generate "Static Pages" In reply to
Hi Henning,

We changed a little bit in version 2.0.2 so that we can have a reasonable structure and users can create a plugin themselves as easily as possible. You should enter : Plugins::Dbsql::PluginName::PluginFunction in Code field when you add a new hook.
Please update your code as below :
Code:
sub page {
# -------------------------------------------------------------------
# This subroutine will get called whenever the hook 'home'
# is run. You should call GT::Plugins->action ( STOP ) if you don't
# want the regular code to run, otherwise the code will continue as
# normal.
#
my ($args) = @_;

# Do something useful here
my $file = $args->{cgi}->{page} || '' ;
$file or return $args;
#check if user passed on some tricks in filename
unless ($file =~ m,^([-\w. ]+)$, and $file !~ m,\.\.,) {
return $args->error("Invalid page $file");
}
# you can add some vars and return to the template here, good luck
GT::Plugins->action ( STOP );
return ($file,{})
}
Regards,
jean
Quote Reply
Re: [jean] Using templates to generate "Static Pages" In reply to
Hi
now it works fine without the "1" file.
Thanks!

henning

Quote Reply
Re: [ktt] Using templates to generate "Static Pages" In reply to
One other issue (I hope doublemint reads this)Wink is how can you get it to return a default page in case someone messes with their browser location field?

ie:
I have do=home&page=some_page.html - that page exists and everything is fine but say for instance a visitor messing around edits the URL in the location field to :

do=home&page=some.html - which doesn't exist and then produces the nasty output of GT::Template::Parser (61974): Unable to open template file blah blah...

It'd be nice if it returned a default page if the static template wasn't found...I looked at Static_Pages.pm but obviously couldn't figure it out (tried some alterations with far less than perfect results)Crazy
Any ideas? Thanks.

easy does it
Quote Reply
Re: [Bearwithme] Using templates to generate "Static Pages" In reply to
Hi,
You can replace the code with :
.....
unless ($file =~ m,^([-\w. ]+)$, and $file !~ m,\.\., and -e "$args->{root}/templates/$args->{cfg}->{template}/$file") {
return $args->error("Invalid page $file");
}
....
That's it.WinkSmile
Cheers,
jean
Quote Reply
Re: [jean] Using templates to generate "Static Pages" In reply to
Quote:
You can replace the code with :

Thanks jean...I'll give it try.


easy does it
Quote Reply
Re: [jean] Using templates to generate "Static Pages" In reply to
Hi

Getting a bit confused now. We had to add a file named "1" top make the first subroutine suggestion by doublemint work. We are using ver2.02

Then tried the next subroutine suggestion so that any page could be parsed and not independently hardcoded. This failed a follows

GT::Template::Parser (1996): Unable to open template file 'f:/webwork/testing/htdocs/cgi-bin/dbsql/admin/templates/test/some_page'. Reason: File does not exist. at f:/webwork/testing/htdocs/cgi-bin/dbsql/admin/GT/Template.pm line 382.

Don't understand procedure for Plug-In if this is the working and preferred solution. is there a tutorial on procedure or some one care to step me through it once in this case?


Thanks

John[/code]


Last edited by:

gatman: Dec 31, 2001, 9:43 AM
Quote Reply
Re: [gatman] Using templates to generate "Static Pages" In reply to
Being as I haven't located a walk-though or tutorial on Plug-in Creation, I have tried to make one for this static web page code

I used the Wizard and did following

1) Create new plug-in named =>StaticPage

2) Version - 1.0

3) Author - Gossamer =>Next

4) Add New Hook = > home => Add New Hook => Next

5) Step 4, 5, 6 & 7- Skipped => Next

6) Edit StaticPage.pm

# Your code begins here! Good Luck!

sub page {
# -------------------------------------------------------------------
# This subroutine will get called whenever the hook 'home'
# is run. You should call GT::Plugins->action ( STOP ) if you don't
# want the regular code to run, otherwise the code will continue as
# normal.
#
my ($args) = @_;

# Do something useful here
my $file = $args->{cgi}->{page} || '' ;
$file or return $args;
#check if user passed on some tricks in filename
unless ($file =~ m,^([-\w. ]+)$, and $file !~ m,\.\.,) {
return $args->error("Invalid page $file");
}
# you can add some vars and return to the template here, good luck
GT::Plugins->action ( STOP );
return ($file,{})
}
# Always end with a 1.
1;

7 Added Link To Exisiting Page In Templates Directory as follows

<a href="db.cgi?do=home&page=homer.html&db=<%db%>">Homer</a>

- Doesn't Work - calls the home page

Help Please - Thanks

John
Quote Reply
Re: [gatman] Using templates to generate "Static Pages" In reply to
Hi John,

Sorry it took me so long to get your message. Could you paste your error message here and we can figure out what happened? Are you using the latest version Dbsql 2.0.2?

Cheers,
jean@gossamer-threads.com
Quote Reply
Re: [jean] Using templates to generate "Static Pages" In reply to
Hi Jean - Thanks, yes - using 2.0.2, and trying to create plug-in named StaticPage per steps above

Error is captains: Error

captains: Error



Invalid action.



Also at Step 3 we have tried

Name: home
Type: Pre
Code: Plugins::StaticPage::
... and ...Plugins::StaticPage::page
Position: First


And, link is as follows:
<a href="db.cgi?do=home&StaticPage=more_details.html&db=<%db%>">More Details2</a>
.. and tried ...
<a href="db.cgi?do=home&page=more_details.html&db=<%db%>">More Details2</a>


Regards- John
Quote Reply
Re: [gatman] Using templates to generate "Static Pages" In reply to
Hi John,

That looks weird, this error happens when you try to call a function that doesn't exist. Could you send me an attached email of your plugin (in admin/Plugins/Dbsql/Installed) to jean@gossamer-threads.com then I can take a look if something were wrong.

Cheers,

jean
Quote Reply
Re: [gatman] Using templates to generate "Static Pages" In reply to
Hi John

Let take a look at install subroutine in your Install.pm and it should be liked:
...
$mgr->install_hooks ( 'StaticPage', [ ['home', 'PRE', 'Plugins::Dbsql::StaticPage::page'] ]);
...

Anyway, you can take a look at the plugin attached file that I made.

Cheers,

TheStone.

B.

Last edited by:

TheStone: Jan 4, 2002, 10:20 AM
Quote Reply
Re: [gatman] Using templates to generate "Static Pages" In reply to
Hi John,

I just took a look at your code. Everything is perfect except wrong plugin name (at Plugin Wizard: Step 3 - Plugin Hooks).. It should be Plugins::Dbsql::StaticPage:: instead of Plugins::StaticPage:: .After fixing this I can't find any error "Invalid action" like you did and it works properly??? Which web server are you using?

Regards,

Jean

> >