On Jun 8,  1:56pm, Perrin Harkins wrote:
> Not quite.  The current version uses its own system of opcodes (!) which
> are implemented as closures.  Compiling to perl code gives much better
> performance, which is why Andy is changing this.

Yep, Perrin's right.  Version 1 compiled templates to tree form.  Items
in the tree were scalars (plain text) or references to directive objects
which performed some processing (like INCLUDE another template, and so
on).

This is actually pretty efficient when you have a limited directive set,
but doesn't scale very well.  For version 1.00 I was more concerned
about getting it functioning correctly than running fast (it was already
an order of magnitude or two faster than Text::MetaText, the predecessor,
so I was happy).  Also it was much easier to develop and evolve the toolkit
with the tree-form architecture than when compiling to Perl, so it had some
hidden benefit.

But the current alpha version of 2.00 compiles templates to Perl code.  A
template like this:

   [% INCLUDE header
      title = "This is a test"
   %]

    Blah Blah Blah [% foo %]

   [% INCLUDE footer %]

is compiled to something resembling

   sub {
        my $context = shift;
        my $stash   = $context->stash();
        my $output  = '';

        $output .= $context->include('header', { title => 'This is a test' });
        $output .= "\nBlah Blah Blah ";
        $output .= $stash->get('foo');

        return $output;
   }

Apart from the benefits of speed, this also means that you can cache
compiled templates to disk (i.e. write the Perl to a file).  Thus you
can run a web server directly from template components compiled to Perl
and you don't even need to load the template parser.  Ideally, it should
be possible to integrate compiled TT templates with Mason components
and/or any other template form which gets compiled to Perl.



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