[Templates] Converting from old template language which allows nested tags into TT template

Andrew Strader astrader@ecnext.com
Thu, 19 Oct 2006 13:29:42 -0400


Robin,

Can you provide examples of code in the old template language which =
would help us understand how nested tag structures are used? Then it =
would be easier to make suggestions about how to transform it into TT =
code.


-----Original Message-----
From: templates-admin@template-toolkit.org =
[mailto:templates-admin@template-toolkit.org] On Behalf Of Robin =
Smidsr=F8d
Sent: Thursday, October 19, 2006 6:44 AM
To: templates@template-toolkit.org
Subject: [Templates] Converting from old template language which allows =
nested tags into TT template

I'm in the process of converting an old (undocumented) template language =

into TT templates. The old template system allow tags within tags, and=20
AFAIK, TT does not allow this.

After initial conversion I end up with a TT template that contains tags=20
like this:

[% popup('begin','[% embed('22478997-9520-4d0f-8abc-f888e2434c81', {=20
type =3D> 'image', mimetype =3D> 'image/gif' } ) %] What have you =
learned',=20
{ event =3D> 'onclick', title =3D> '[%=20
link('22478997-9520-4d0f-8abc-f888e2434c81') %]' } ) %]

PS: popup() is a function to create a popup-window (in HTML), embed()=20
returns the correct <img> tag, and link() returns a pretty formatted =
<a>.
PS2: I know there are some bogus single-quotes in that statement aswell. =

Hopefully it will not be a problem with my proposed solution.

My first initial reaction was to try and remove the inner tags, add a=20
terminating single quote and use a string concatenation character to=20
include the output of the embed() function directly into the string. I=20
researched the mailinglist, but found no string concatenation character=20
in TT. Only some references to "." or "~" that didn't work. But I found=20
a reference that suggested [% INCLUDE [% value %] %] be changed to [%=20
INCLUDE $value %] which should work. This got me thinking that something =

like this should be possible:

[%
 out1 =3D embed('22478997-9520-4d0f-8abc-f888e2434c81', { type =3D> =
'image',=20
mimetype =3D> 'image/gif' } );
 out2 =3D link('22478997-9520-4d0f-8abc-f888e2434c81');
 popup('begin',"$out1 What have you learned?", { event =3D> 'onclick',=20
title =3D> "$out2" } )
%]

Of course, since the old language supported nesting of tags there must=20
some smart algorithm that reads the nesting in reverse (or something)=20
and outputs numbered variables to include in the outer tag.

My regexp/LALR knowledge is a bit too weak to be able to understand=20
exactly how I should write a parser that can transform the first code to =

the second. Also be alert that there can be multiple occourences of=20
similar outer tags in the same text. So some sort of s//g thing is=20
needed to transform them all, and of course the numbered variable=20
mustn't overwrite the ones who have been defined in the earlier tags=20
(document global counter). The routine doesn't need to be super-fast, as =

it will only be used once per document, and the resulting TT template=20
will be stored for later display.

What I do now is just to go through the old template and exchange each=20
occurence of the old template with the new TT code, and I was planning=20
on using this final algorithm to fix any accidential nesting problems=20
that had occured.

Am I looking at this problem all wrong? Any hints/code=20
examples/suggestions are welcome.

Regards,
Robin Smidsr=F8d


_______________________________________________
templates mailing list
templates@template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/templates