On Nov 11,  4:59pm, Robin Szemeti wrote:
> many of the rows have various html elements
> included or excluded depending on what does or does not happen in the
> data. I can;t see how you could accomplish that functionality with a
> template system .. but I could be worng.

Everything depends on your definition of "what does or does not happen
in the data", but here's one made-up example that hopefully illustrates
some of the stuff you can do.

  [% USE dbi('dbi:mysql:blahblah') %]

  [% FOREACH user = dbi.query('SELECT * FROM users') %]
     [% INCLUDE user_info %]
     [% INCLUDE admin_commands IF user.isadmin %]
     [% INCLUDE project_info
        FOREACH project = dbi.query("SELECT * FROM projects
                                     WHERE manager='$user.id'")
     %]
     [% INCLUDE something_else
        IF my_fiendishly_clever_code_thinks_I_should_for_this(project) %]
  [% END %]

The last example is the catch-all.  If there's something more complex that
you can't test with a simple IF, etc., then you just write your own Perl
sub, bind it to a template variable, and then pass your data to it as and
when you need to.  Your code can be as fiendishly clever as you like and
return pretty much any kind of value which you can then do some more with.

    [% IF the_average_monthly_rainfall_in_peru_is_more_than(100) %]
       ...

    [% FOREACH person = all_the_people_in_london_pm_who_drink('guiness') %]
       ...

    [% WHILE (message = what_jim_morrison_told_me_in_a_dream) %]
       ...

If you want to get more clever still, you can write a plugin which can
make callbacks into TT to process other templates.

    package MyPlugin;
    use base qw( Template::Plugin );

    sub new {
        my ($class, $context, $data) = @_;
        foreach my $item (@$data) {
            $item = do_something_very_clever_with($item);
            $context->include('table_row', { item => $item });
        }
        return 1;
    }

meanwhile, in a template...

    [% USE MyPlugin([ 'foo', 'bar', 'baz' ]) %]

    [% BLOCK table_row %]
       <tr><td>[% item.field1 %]</td><td>[% item.field2 %]</td></tr>
    [% END %}

As this should hopefully illustrate, you can call Perl code from
within templates and pass data to and fro.  You can also process
template components from within Perl code, if you really want to, but
still define them in the template where they should be (out there in
look-and-feel land) rather than have them hidden away in Perl code
(functionality corner).

Hope this help.  If it doesn't then feel free to call for clarity,
or ask on the TT mailing list where there are many helpful folk who
do lots of clever stuff like this.

Cheers

A






-- 
Andy Wardley <[EMAIL PROTECTED]>   Signature regenerating.  Please remain seated.
     <[EMAIL PROTECTED]>   For a good time: http://www.kfs.org/~abw/

Reply via email to