Chris Winters wrote:
> 
> * [EMAIL PROTECTED] ([EMAIL PROTECTED]) [000608 11:07]:
> > I'm curious Matt, as opposed to what?, reparsing the template each
> > run?  Clearly reparsing would be a big loser in terms of performance.
> >
> > But what other technique could be used..., hrm.., without direct
> > control over the pipe, I really don't think it would get too much
> > better than this.  I mean, you could yank out sections and stuff it
> > into an array that would be like: text, coderef, coderef, text, etc.
> > Like in an ASP template you would parse the file, grab sections
> > between <% %> and eval it as a code ref, and stuff it into your array.
> > But this would probably not work specifically in ASP's case, but you
> > might be able to pull it off in Embperl.  (Unless the array itself
> > could also point to arrays, etc.)  Overall..., I think compiling it
> > directly makes a lot more sense in 99.999% of template languages...,
> > otherwise you'd end up putting too many restrictions on the template
> > language itself.
> >
> > Hmm..., sort of an interesting question, what ways could be utilized
> > in order to maximize speed in template execution.  I thought about
> > this a while ago, but after the fact I have to agree with Matt...,
> > just evaling each template as a package, or a code ref would be a
> > lot quicker, and if you could cook up another scheme, the resulting
> > code complexity might not pan out to be worth it.
> 
> The newest version of Template Toolkit (currently in alpha) supports
> compiling templates to perl code. See about 2/3 of the way down the
> the README at www.template-toolkit.org. Why reinvent the wheel? :)

My original question was not related to templates (I'll use embperl for
that) - the area I was trying to explore was how to read a template (all
HTML with a few <!--TAGS--> in it) and the sub in the new content.

My pages usually have serveral templates - or one larger template with
comments arround iterative bits (tables) so that I end up with a main
template which may look something like:

<html>
<head>
<title>
<!--TITLE-->
</title>
</head>
<body <!-- BODY -->>
<table>
<!--TABLE-->
</table>
</body></html>

and a table template:

<tr><td valign='foo' color='bar'><!--CELL--></td></tr>

The Designers / HTML coders I work with can then chop and change these
templates - I have made a template loader that strips the iterative
table content out into a seperate template, so that they can code a fake
entry for design reasons - and I then strip it out (looking for some
special HTML comment tags).

In a startup.pl The code then looks somat like this forgive the probably
uncompliable perl etc ... this is for explaination purposes:

use vars (MAINTMP TABLETMP);

$MAINTMP = &load('main.tmp');
$TABLETMP = &load('table.tmp');


sub load {

        my $file = shift;
        my $html = '';

        open (TEMPLATE, $file) || die ('Cant open : ', $file, $!);
        while (<TEMPLATE>) {
                chomp;
                $_ =~ s/^\s+//;
                $_ =~ s/\s+$//;
                $html = $html . $_;
        }
        close (TEMPLATE)  || die ('Cant close : ', $file, $!);
        return $html; 
}


then in a handler:

sub makeTableContents {

        # assumes @_ is a nice list in order of values
        # to insert into the table ..
        my $template = $TABLETMP;
        my $tablehtml;
        foreach (@_) {
                $template ~= s/<!--CELL-->/$_/;
                $tableHTML .= $template;
        }
        return $tableHTML;
}


sub doStuff {
        
        # assumes that it is passed in the return value from
        # makeTableContents and a title
        my $tableHTML = shift;
        my $title = shift;
        my $template = $MAINTMP;
        
        $template =~ s/<!--TITLE-->/$title/;
        $template =~ s/<!--TABLE-->/$tableHTML/;

        return $template;
}

Then when I want to send it I just print the return value of doStuff

This may not be OO - but it is simplistic, and although not benchmarked,
should be fast.  I have seen another OO ish implentation, that uses a
hash a bit like  Mats example - this allows more flexibility as all you
need do is add the tag to the HTML, and then add the tag to the hash -
then when you do the regex it gets put in.

Has anyone any suggestions as to speeding this up - yet keeping it
simple - I have played with referances to avoid all the variable copying
etc . ?

Thinking about it Mat's example is far more scalable, and reusable.

I am enjoying this thread ;-)

Greg Cope


> Chris
> 
> --
> Chris Winters
> Internet Developer    INTES Networking
> [EMAIL PROTECTED]    http://www.intes.net/
> Integrated hardware/software solutions to make the Internet work for you.

Reply via email to