Mystical Monks,

This may be the abc's of Template Toolkit for many, but I thought I'd share my discovery of TT for those who might also be discovering it in future.

I've recently been very pleasantly surprised by how using TT simplified and condensed the creation of a dynamically generated interface that can present multiple mixtures of information and functions to multiple user types.

Imagine you have one database of business information. Typically a user is interested in outputting a long report of attributes for case #'s in a table. Each row = a case and its columns of attributes.

You have three user types: admin, staff, client. Each is to have access to varying levels of the data and different functions. Clients can see certain things. Staff can see others,receive staff-only messages, and change some things. Admin can view and change anything. For example.

Well, before TT, you were going to have to have multiple routines in your Perl script to sort out what elements to output, with lots of ugly statements like:

print "<table>\n"; foreach $item (@items_list) { print "<tr><td>"; if ($user_type eq 'admin' || $user_type eq 'staff') { print "<a href='db_script.pl?item=$item&action=show_details' +>"; } print $item; if ($user_type eq 'admin' || $user_type eq 'staff') { print "</a>"; } print "</td></tr>\n"; } print "</table>\n";
Yuck.

No, instead you're going to make this beautfilly clean separation between data and mark-up.
Script:

use DBI; use Template; # a little extra shown for cliarity - first get user type $sql = "SELECT user_type FROM usersTable WHERE (username = $user_name) +; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr) +; $sth->execute() or die("Could not execute!" . $dbh->errstr); $user_type = $sth->fetchrow_array(); $sth->finish; # get the attributes of the item ids being requested, in this example, + corresponding to all items with attribute x = y $sql = "SELECT item_id, att_2, att_3....att_n FROM theTable WHERE (att +_x = 'y'); $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr) +; $sth->execute() or die("Could not execute!" . $dbh->errstr); # build a data structure to store them while ($item_id, $att_2, $att_3....$att_n) = $sth->fetchrow_array()) { $data{$item_id} = { att_2 => $att_2, att_3 => $att_3, ..... att_n => $att_n } } $sth->finish; #user TT to output the results $template_file = "my_template.htm"; # this passes the whole data hash to the template $vars = { user_type => $user_type, data => \%data } $template->process($template_file, $vars) || die "Template process fai +led: ", $template->error(), "\n";
Much nicer. Now in your template:
#insert appropriate toolbars depending on user type [% IF user_type == 'admin' %] [% INCLUDE admin_function_buttons %] [% ELSIF user_type == 'staff' %] [% INCLUDE staff_function_buttons %] [% ELSE %] [% INCLUDE client_function_buttons %] [% END %] <table class="datatable"> #loop through data hash [% FOREACH item_id IN data.keys.sort %] <tr> <td> #insert links to functions on items attributes depending on user +type [% IF user_type == 'admin' || user_type == 'staff' %] <a href='db_script.pl?item=[% data.$item_id.att_1 %]&action= +show_details'> [% END %] data.$item_id.att_1 [% IF user_type == 'admin' || user_type == 'staff' %] <a> [% END %]</td> <td>[% data.$item_id.att_2 %]</td> <td>[% data.$item_id.att_3 %] # display messages depending on condition and user type [% IF data.$item_id.att_3 >= 100 && user_type == 'staff'%]Hi +gh stock - move these suckers!![% END %] </td> ..... <td>[% data.$item_id.att_N %]</td> </tr> [% END %] </table>

In other words, you've made three user interfaces with one Perl script and one template, and that will automatically share look and feel with any stylesheet or template change.

Much thanks to the creators of TT.




Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re: Multi-user-type interfaces made easy with Template Toolkit
by Jenda (Abbot) on Apr 16, 2007 at 14:42 UTC
    $sql = "SELECT user_type FROM usersTable WHERE (username = $user_name) +; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr) +; $sth->execute() or die("Could not execute!" . $dbh->errstr);

    Please don't. I guess this is not your exact code (it would not even compile due to missing doublequote), but still I think it's better to warn others. Either make sure the $user_name is properly escaped or even better use placeholders:

    $sql = "SELECT user_type FROM usersTable WHERE (username = ?)"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr) +; $sth->execute($user_name) or die("Could not execute!" . $dbh->errstr);
    This is not only safer, but also most likely quicker. This way the database has a chance to cache the statement. The way you had it, the server had to recompile and recreate the execution plan whenever you needed to run the same query for a different user.