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.

In reply to Multi-user-type interfaces made easy with Template Toolkit by punch_card_don

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.