Folks,

Apologies in advance - a TT newbie lurker, asking what are probably 
obvious [and long, sorry!] questions to you all. Please point me to 
documentation that you feel is relevant - I have googled and read quite 
a lot, obviously not enough!

I am trying to build some basic template infra that will present tables 
of data, with some not-too-complex HTML interaction - eg sorting 
columns, some cells are links to other pages or on-site popups, cells 
have tooltips etc.

The data contains strings, numerics and dates which should be 
prettified, commified, rounded, properised, etc based on a 
data-dictionary (DD).

I'm sure that this is done many times in TT land.

Here is a first cut snippet from my datatable.tt2

<table class='dt_table' >
   <tr class='dt_th'>
   [% FOREACH column IN datatable.cols -%]
     <th class='dt_th' >
       <a class='dt_th[% IF datatable.order == column %] sorted[%END%]'
          href='[% Catalyst.uri_for("${page}?oin=$column") %]'>[%
           column | descr | ucwords %]</a>
     </th>
   [% END -%]
   </tr>
   [% IF datatable.countRows -%]
   [%  FOREACH row IN datatable.rows -%]
   <tr>
     [%  FOREACH column IN datatable.cols -%]
     [%    IF datatable.editable( column ) -%]
     <td><input name='[% column %]'
        value='[% row.$column.formatted( column ) | html %]'></td>
     [%    ELSE -%]
     <td [% row.$column.css( column ) %]>[%
        row.$column.formatted( column ) | html -%]</td>
     [%    END -%]
     [%  END -%]
     </tr>
   [%  END -%]
   [% ELSE -%]
     <tr>
       <td class=error colspan=[% datatable.countCols+1 %]>No [%
         datatable.description  %] details found</td>
     </tr>
   [% END -%]
</table>

Which kind-of works. By way of explanation, descr() and ucwords() are 
filters are defined in my View class (this is Catalyst wrapped) e.g.;

__PACKAGE__->config({
     FILTERS => {
       descr => sub {
           return join(" ", split/_/, $_[0]);
         },
       ucwords => sub {
           $_[0] =~ s/\b(.)/\U$1/g;
           return $_[0];
         },
     }
   });

and I have used SCALAR_OPS for ->formatted() e.g.

# formatting for named scalar value
$Template::Stash::SCALAR_OPS->{formatted} = \&scalarFormatted;
sub scalarFormatted {
     my $value   = shift;
     my $name    = shift;
     ...
     return $value;
}

for use in templates as:
   [% row.$column.formatted( column ) | html %]

but now I want to automatically render the column as:

1) a link ( cannot use html filter anymore, have to do this inside the 
sub? )

2) input for editing (need additional attrs like size, type of input - 
eg text, or even a <select> for enums from DD etc etc )

3) add tooltips to the cell if appropriate (decided by DD)

4) add onclick events if appropriate (determined by DD)

and being a perl-head, I am struggling not to throw out the template 
with the baby and just revert to a Perl pure view.

I think I'm going to end up with templates containing a large list of TT 
IF...ELSE IF...

Can you please make recommendations for how this should/could/would be 
implemented well in TT2?

Do you pass in the DD as an object to ask it questions? etc.

Many many thanks!

Jeff,
London, UK

_______________________________________________
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to