On 7/21/05, Will <[EMAIL PROTECTED]> wrote:
> I love what all 3 of these modules bring to the table, Template Toolkit,
> Class::DBI, and CGI::Application.
>
> I admit I'm new fairly new to all of them, but I have working scripts of
> each module on their own. I'm yet to find the best way to get them all
> to work together in the best way possible.
>
> Any suggestions? Could you explain to me how to best use these modules
> together, or point me to some example already on the web?
Others have already mentioned the TT plugin for CGI::Application, but
other than that, you don't really need anything else to get those
three modules to work well together.
Class::DBI doesn't really need to be integrated into CGI::Application
at all, because you can call any of the Class::DBI modules you've
created directly from your runmodes. There is no integration
necesary.
If you want an example runmode of how these modules interoperate, here
is a very simple one:
package MyDemo;
use CGI::Application::Plugin::TT;
use CGI::Application::Plugin::AutoRunmode;
use CDBI::Books;
sub books : Runmode {
my $self = shift;
my $tt_vars = shift || {};
$tt_vars->{books} = [CDBI::Books->retrieve_all];
return $self->tt_process($tt_vars);
}
Notice that I didn't pass a template name to tt_process, as it
automatically builds a filename based on the module name, and the name
of the runmode (you override that by just passing it a template name).
So it will try to load MyDemo/books.tmpl, and we might find the
following in that template:
<h1>Book list</h1>
<ul>
[% FOREACH book IN books %]
<li>[% book.name %] by [% book.author %]
[% END %]
</ul>
I also use the Template::Plugin::URL module to generate links for me
in the template:
[% USE url( c.query.url( '-absolute'=1 ) rm=c.get_current_runmode ) %]
<a href="[% url( rm='view_book' book_id=book.book_id %]">[% book.name %]</a>
The USE statement loads the plugin, and sets the default URL to be the
current script and current runmode (so just calling [% url() %] in a
template will give a URL that will reload the current runmode). I
usually put the USE statement in a PRE_PROCESS template that
Template::Toolkit loads automatically before every template that gets
processed (see the Template Toolkit docs for more info).
If you use the above code you will need to add the following method to
your CGIApp module so you get the 'c' object, which is just the
CGI::Application object.
sub tt_pre_process {
my $self = shift;
my $file = shift;
my $vars = shift;
$vars->{c} ||= $self;
return;
}
That will automatically put your CGIApp object into the template
parameters for every template that is processed. That way you have
access to the query object, and some other convenience methods from
within your templates. For example, if you have the users name in the
session, and you used the CGI::Application::Plugin::Session module,
you could add [% c.session.param( 'username' ) %] to display the
username in the template.
There are lots of other little tips and tricks that make life easier
when using these modules together. I think I have given away enough
for one message ;)
Good luck, and if you have any other questions, all three of these
modules are often discussed on the cgiapp mailing list.
Cheers,
Cees
_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates