cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonas Ekstedt <ekst...@ibg.uu.se>
Subject Implementation of a tag attribute language
Date Sun, 05 Dec 2004 13:09:09 GMT
Hello

The recent discussions about tag attribute languages has been very
interesting and enlightening. To make it more concrete here are a few
ideas on how to actually implement these concepts.

TALGenerator
------------
* Extends the generator in the template block.
* Does not support tag bundles (ie taglibs)
* Only treats elements containing a tal:do attribute in a special way.

A new TALToken is introduced (for an introduction to tokens see
http://www.mail-archive.com/dev@cocoon.apache.org/msg25040.html). Here's
an example of how a TAL template is compiled:

<table>
  <tr tal:do="forEach(listOfBeans)">
    <td>${it.beanProperty1}</td>
    <td>${it.beanProperty2}</td>
  </tr>
</table>

=> (ignoring whitespace nodes) =>

PlainElementToken  localName="table"
TALToken           localName="tr", do="forEach(listOfBeans)"
PlainElementToken  localName="td"
ExpressionToken    expression="it.beanProperty1"
PlainElementToken  localName="td"
ExpressionToken    expression="it.beanProperty2"

TALToken might look something like:

public class TALToken {
  ...

  public void invoke(ScriptContext context) {
    if (getDoCommand().equals("forEach")) {

      Iterator it = evaluateToIterator(getDoParameter(0));
      while (it.hasNext()) {
        context.getVariables().put("it", it.next());
        context.getConsumer().startElement(uri, localName, qname, attrs)
        invokeBody(context);
        context.getConsumer().endElement(uri, localName, qname);
      }

    } else if (getDoCommand().equals("if")) {

      boolean test = evaluateToBoolean(getDoParameter(0));
      if (test) {
        context.getConsumer().startElement(uri, localName, qname, attrs)
        invokeBody(context);
        context.getConsumer().endElement(uri, localName, qname);
      }

    } else if (...) {
      ...
    }
  }
}

I think a TALGenerator that is limited in scope (ie only supports a
few control structures like if, forEach etc) would be quite easy
to implement if it is built on top of the current template block.

Cheers Jonas

Mime
View raw message