I need some assistance with designing a system using TT2(CGI)+DB. As an
example of the issue, assume the system needs users to login before
using
it, and that the login page needs state information in case the user
started at a particular page, needs to authenticate/join and so comes to
that page, and then want to go back to the page they started. All
fairly
simple and familiar (although my wording isn't...)
All of the logic to determine this can occur in templates
themselves, or it can occur mostly in the perl program. I'd like some
suggestions of where the division is more sensible.
Three possibilities (that I can think of!):
1. Using plugins. The template - simplified a lot :-) - could be:
login.tt2:
----
[% USE mine %]
[% IF mine.hasAuthenticated %]
[% INCLUDE mine.showNextPage %]
[% ELSE %]
<FORM ACTION=mine.thisScript>...</FORM>
[% END %]
showNextPage is a scalar with either a default or where the user
came from before needing to login.
hasAuthenticated could be a function eg.
Authenticated(idUserProvided)
2. Like 1. but using variable instantiated in the perl (basically just
remove the 'mine' in the example 1.
3. Using DB in the template:
login.tt2:
----
[% USE DBI %]
[% authenticated = 'no' %]
[% DBI.connect(dsn, specialuser, secretpass) %]
[% query = DBI.prepare('SELECT * FROM users WHERE userid = ?') %]
[% FOREACH user = query.execute(idUserProvided) %]
[% authenticated = 'yes' %]
[% END %]
[% IF authenticated %]
[% INCLUDE showNextPage %]
[% ELSE %]
<FORM ACTION... <- as before
[% END %]
----
Example 1 and 2 requires the plugin or CGI script (thisScript) to
instantiate lots of variables which may or may not be used in the
template. Example 3 still requires the script to do some, but
essentially it's more for setting defaults.
So, ignoring any basic TT2 syntax errors (:-), which way would people
suggest? Given that this is a simple example, real systems would
have many of these issues. Do you do as much of the instantiating
as possible in the Perl, and truly let the template worry only about
looking good? Or is it minimal Perl, maximum use of the Template
programming/plugins? Theory (IIRC) says do the former, but I'm
wondering about practical experiences people have had, with TT2 in
particular.
Another way (!) is to have a Perl module with various functions like
authenticate, program-specific DB access etc. The CGI script could
call these functions (like 2.), but a plugin could also offer them
to the template providing flexibility. However, this just defers the
basic question which is where is the best place for the functionality
to be put...
Thanks for any help,
Robert
--
Robert McArthur CRC for Enterprise Distributed System Technology
BSc(Hons) Ph. +61 7 3365 4310 Brisbane, Australia
MInfTech Fax +61 7 3365 4311
Grad.Cert.Ed. [EMAIL PROTECTED]