Forum OpenACS Development: Re: new templating model: ideas, questions

Collapse
Posted by Tom Jackson on

The change from [set arg arg] to <tcl:set arg arg /> is easy, it can even be a compile time option (for the compiler not the scripts). However, representing the args as attributes might be a problem. The issue is the quoting rules for XML attributes conflicting with the quoting and other rules for Tcl args.

The model I am using is to allow the lexer find valid arg tokens and pass them to the parser to validate the grammer. Neither of these parts can verify the validity of the arguments (neither can the Tcl parser, arguments are opaque). I do examine the arguments a little. I verify they do not contain embeded commands, but combining two quoting schemes might confuse more than help the situation.

I looked into XSL, as others have pointed out, it is complex and unfriendly. It is also a full programming language, and could access resources based on what is contained in the XSLT. But the main issue is that it isn't apparent how the XML document would be created from the Tcl scripts, since the model with Tcl is to define variables and execute commands. You would still be stuck with the task of creating an XML document, or DOM from Tcl structures. My goal was to use the Tcl language as much as possible, and as safely as possible so that display code can be completely separated from other types of code. I also want to be able to use it in as many places as possible, like within a database or in procedures where templates might be stored in the database.

The webserver tclhttpd uses Safe-Tcl and a simple templating language. Files ending in .tml are run through subst. Unfortunately this eliminates all the important selection and iteration commands that make a templating language useful.

Collapse
Posted by Andrei Popov on

I am not suggesting using XSL as such -- only following the approach, as this creates a valid XML file. I guess that a sample template could be something like this (using original example):

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>@title@</title> <!-- keep @var@'s -->
</head>

<body>
  <p>Got a list @list@</p>
  <p>Looping over list:</p>
  <ul>
    <tcl:foreach l="@list@">
      <li>@l@</li>
    </tcl:foreach>
  </ul>

  <p>Got an array @array@</p>
  <p>Looping over Array:</p>
  <table>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>City</th>
    </tr>
    <tcl:foreach a="@array@">
      <tr>
        <td>@a(name)@</td>
        <td>@a(age)@</td>
        <td>@a(city)@</td>
      </tr>
    </tcl:foreach>
  </table>

</body>
</html>

When <tcl:...> tag is parsed in this case, XML quoting (single or double quotes) should simply be stripped before evaluating quoted expression. Then an expression should be evaluated using Tcl evaluation rules. Using namespace prefix (tcl:) should actually help parser a lot, since it would no that only these tags need to be expanded, others can remain as they are.

I am sure that this is *very* simplistic, but it may just work... Just don't think of it as "implement templating system in XSL", but rahter "borrow XSL syntax for template definition language".