ivan <[EMAIL PROTECTED]> writes:
> > > > > Is TT2 a "little language" or "eval" templating system?
> > > >
> > > > It can be anything you want it to be. ;-)
> > >
> > > That's a silly answer.
> >
> > There are two methods to embed perl code in the template.
>
> But - and this is the important bit - can you do the *reverse* - can you
> embed TEMPLATE in perl code? The equivalent of (for example) Mason's:
>
> % foreach $option ( @options ) {
> <OPTION><% $option %>
> % }
Yes. TT is flexible. I can't hope to imagine all the ways to do it.
But you could process a template from perl:
[% BLOCK optrow %]
<option>[% myopt %]
[% END %]
[% PERL %]
foreach my $option ( 'a', 'b', 'c' ) {
print $context->process('optrow', { myopt => $option });
}
[% END %]
----
You coule insert a template in the perl code, but since they are
evaluated before the code is executed, this would only give the value
from before the perl block. (But it can be done with lazy evaluation,
as in making myopt() to a function call.)
[% PERL %]
foreach my $option ( 'a', 'b', 'c' ) {
$stash->set( myopt => $option );
print "<option> [% myopt %]\n";
}
[% END %]
----
You couls also possibly bind a variable to the stash and thus skip the
set() part...
---
There are many more ways. But the *prefered* way would be to just do:
[% FOREACH myopt = [ 'a', 'b', 'c' ] %]
<option>$myopt
[% END %]
> Note that if eval()ed as stand-alone Perl, both of the % sections would
> generate syntax erros.
Speaking of that... You CAN do it almost exactly like your example,
with the RAWPERL option. (Especially if you replace that set() by
binding $option.) But this is a bit slower in execution than above:
[% RAWPERL %] foreach $option ( 'a', 'b', 'c' ) {
$stash->set( myopt => $option );
[% END %]
<option>$myopt
[% RAWPERL %] } [% END %]
----
More than that. Since the whole Template Toolkit is modularised, you
can define not only your own TAGS ( [% .. %] ) but also your own
GRAMMAR and/or PARSER. Using these options. I guess you could
ultimately write your example like this:
% foreach $option ( @options ) {
<OPTION><% $option %>
% }
This is a part of the documentation about the GRAMMAR option:
The Template::Grammar file is constructed from a YACC like
grammar (using Parse::YAPP) and a skeleton module template. These
files are provided, along with a small script to rebuild the
grammar, in the 'parser' sub-directory of the distribution. You
don't have to know or worry about these unless you want to hack
on the template language or define your own variant. There is a
README file in the same directory which provides some small
guidance but it is assumed that you know what you're doing if you
venture herein. If you grok LALR parsers, then you should find it
comfortably familiar.
----
It's not only that you CAN do everything. The issue here is that many
things becomes MUCH easier with The Template Toolkit.
And this at the same time as the templates is also easier to read and
write and faster to execute.
Give me any template in Mason, and I can show you a better way to do
it in TT2.
One more example. Let's say that the options should be HTML escaped:
[% FOREACH myopt = [ 'a', 'b', 'c' ] %]
<option>[% myopt | html %]
[% END %]
> > > Personally, I favor the "eval" templating systems
> >
> > Why?
Most of the time it's better to separate
- data
- configuration
- logic
- presentation
http://www.tt2.org/docs/plain/Manual/Intro.html
--
/ Jonas - http://jonas.liljegren.org/myself/en/index.html