[% PRINT myhash %] [% PRINT myobject %]
[% DO header title="blah blah" %]
[% mytext = TEXT %] This is some text. It can contain [% directives %] but they are ignored. It is always processed as if via INSERT. However, it may be a requirement that the content is syntactically correct... [% END %]
[% XML %] <menu title="My Menu"> <item text="Item 1" link="blahblah.html"/> <item text="Item 2" link="yadayada.html"/> <item text="Item 3" link="yodayoda.html"/> </menu> [% END %] same as: [% WRAPPER menu title="My Menu" %] [% INCLUDE item text="Item 2" link="blahblah.html" %] [% INCLUDE item text="Item 2" link="yadayada.html" %] [% INCLUDE item text="Item 2" link="yodayoda.html" %] [% END %]
Multiple FOREACH targets
[% FOREACH (key, value) = myhash %] [% FOREACH key, value = myhash %] [% FOREACH key value = myhash %]Could it also be possible to specify multiple lists which should be iterated in lockstep, e.g. (not sure of syntax yet...)
Lockstep Iterators
[% FOREACH (col, product) = (colours*, products) %] [% FOREACH col, product = colours*, products %]
Note how we append *
to
colours
to indicate that the values
should repeat if they finish short of the
products
list (e.g. loop).
It might also be a good idea to change the default iterator name from 'loop' to 'each' or even 'EACH'. Further still, the parser might also support directives of the form FOR_NAME or FORNAME to allow a different iterator name to be specified.
Named FOREACH Loops
[% FOREACH user = userlist %] [% IF EACH.last %] ... [% END %] [% END %] [% FORGROUP group = grouplist %] [% FORUSER user = userlist %] [% IF GROUP.size > 5 %] [% LAST GROUP %] [% END %] [% END %] [% END %]
END
directive as, for example, END FOR
,
END_FOR
or ENDFOR
and
have the parser accept it and verify that it
correctly terminates the right block type.
[% IF foo == bar %] [% FOREACH item = things %] ... [% END FOREACH %] [% END IF %]
qw[ ]
operator should be supported for
creating lists of quoted items.
[% people = [ 'tom', 'dick', 'larry' ] %] vs [% people = qw[ tom dick larry ] %]
[% people = qw( tom dick larry ) %](NOTE: parser now supports this with all combinations of parentheses).
Slicing
[% (id, name, email) = user.[ 'id', 'name', 'email' ] %] [% (id, name, email) = user.qw[ id, name, email ] %](NOTE: stash already supports this, thanks to Craig's patch. Parser doesn't yet support [ ] as a varnode, but probably should. Also want to support slices for object methods.
[% CONSTANT title = 'My Title' version = 3.14 %](NOTE: got this working in the parser, but need to think about how constants can be shared across template boundaries).
STASH
should create a localised stash
with a pre-defined set of variables. These mask any
previously defined variables.
[% STASH title = 'My Title' version = 3.14 %]
[% STASH_FAST title = 'My Title' version = 3.14 %]Thus,
CONSTANT
is just a special kind of
stash facility which intercepts all directives at compile
time and returns values.
[% STASH_CONSTANT title = 'My Title' version = 3.14 %]Same as:
[% CONSTANT title = 'My Title' version = 3.14 %]
[% file = http://www.tt3.org/blahblah.html %] [% INCLUDE block:header %] [% INCLUDE file:header %]
[% FOREACH item = dbi:query( my.sql.query ) %] ... [% END %] [% xmldom = xml:dom( my.xml.file ) %]
[% my = user:abw %]
[% me = user('abw')
%]
or [% me = user.abw %]
which
could return any kind of data. Thus you should be able to
[% INCLUDE user:fred %]
and have it Do
The Right Thing in presenting the 'fred' user via the
appropriate template. In contrast, [% INCLUDE $user.fred %],
means a different thing altogether and may not even be
valid.
DBI
facility might define
the dbi:xxx
resource, the SQL
directive and the TT3.dbi
context.facility
reference.
foo | lower
vs
foo.lower
[% INCLUDE x #DEBUG y
#END_DEBUG %]
when '#DEBUG' and '#END_DEBUG' mark
new start/end tokens. Could we implement aspect weaving
with such an extensible set of directives like this? (remember
mrp's pre-processing DEBUG hack?)
[% BLOCK post_chomp=1 %] ... [% END %]
[% PARSER start_tag = '[*' end_tag = '*]' %] [% PARSER.tag_style = 'star' %] [% mydir = "${PARSER.start_tag} INCLUDE header ${PARSER.end_tag}" %]
The parser should accept {
... }
as an alias for ;
... ; END
.
[% IF a == b { INCLUDE foo } ELSE { INCLUDE bar } %]
[% IF a == b; INCLUDE foo; ELSE; INCLUDE bar; END %]
This should work across directive tags.
[% IF a == b { %] ... [% } ELSE { %] ... [% } %]
[% BLOCK header { INCLUDE html/header } %]When specified anonymously, a BLOCK is processed as per usual and the output returned.
[% text = BLOCK { INCLUDE html/header } %]Same as in TT2:
[% text = BLOCK; INCLUDE html/header; END %]
[% PERL %] my $foo = 10; print "Output! foo: $foo\n"; [% END %]Or
[% PERL { %] my $foo = 10; print "Output! foo: $foo\n"; [% } %]When specified with a name, a Perl subroutine is defined and assigned to the variable named.
[% PERL title %] my $title = shift; return "<title>$title</title>\n"; [% END %] [% title('Hello World') %]
[% CONTEXT splash %] . . . [% END %]