cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonas Ekstedt <>
Subject A template transformer
Date Tue, 16 Nov 2004 18:19:20 GMT

I have created a template transformer similar to JXTemplate and wondered
if you would be interested in it. It can be found at:

I'll put it in BugZilla as soon as it gets online again. 

* unzip and copy the blocks (conversion and template) to src/blocks
* Apply cocoon.patch
* ./
* start servlet
* surf to http://localhost:8888/samples/blocks/template/

The TemplateTransformer was based very much on how the TagTransformer
works. Developers can extend the functionality by writing Tag classes.
The transformer is configured in sitemap.xmap where the user can
register tags to different namespaces:

<map:transformer name="template"
  <tag uri="http://core" name="if"

If there are many tags, the configuration can be put in an external file
and included like this:

<map:transformer name="template"

To wet your appetite I have listed a few constructs below:


<core:for var="i" begin="1" end="10" step="2">

Looping over collection:

<core:forEach var="date" items="${collectionOfDates}">

Note that the date is converted to short form using convertors from the
conversion block.


Looping over tokenized string:

<core:forEach var="letter" items="A B C D">


I've never particularly liked choose/when so I implemented 
if/elseIf/else instead.

<core:if test="${1 == 2}">
  Not shown
<core:elseIf test="${1 == 3}">
  Not shown
  This is shown


<table xmlns:m="macros">
  <core:macro namespace="macros" name="row">

  <m:row a="1" b="2"/>
  <m:row a="3" b="${var}"/>


<core:import uri="somefile.xml"/>

NB. Tags in the imported file are evaluated as well.


It is really easy to extend the transformer with your own tags. The Tag
interface looks like:

public interface Tag {

    public void setup(TemplateTransformer transformer,
                      ConvertingEvaluator evaluator, 
                      MapStack variables, 
                      Attributes attributes);
    public void start();
    public void end();

Normally you would extend AbstractTag and only implement start() and
possibly end(). 

Cheers Jonas

View raw message