cocoon-dev mailing list archives

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

I have created a template transformer similar to JXTemplate and wondered
if you would be interested in it. It can be found at:
http://home.student.uu.se/j/joek8725/template.zip

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

Installation:
* unzip and copy the blocks (conversion and template) to src/blocks
* Apply cocoon.patch
* ./build.sh
* 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"
      src="org.apache.cocoon.template.TemplateTransformer">
  <tag uri="http://core" name="if"
    src="org.apache.cocoon.template.tag.core.IfTag"/>
</map:transformer>

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

<map:transformer name="template"
      src="org.apache.cocoon.template.TemplateTransformer">
<include>resource://org/apache/cocoon/template/tag/core/tags.xml</include>
<include>resource://org/apache/cocoon/someothertags.xml</include>
</map:transformer>

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

---
Looping:

<core:for var="i" begin="1" end="10" step="2">
  ${i}
</core:for>

---
Looping over collection:

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

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">
  ${letter}
</core:forEach>

---

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

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

---
Macros:

<table xmlns:m="macros">
  <core:macro namespace="macros" name="row">
    <tr>
      <td>${a}</td>
      <td>${b}</td>
    </tr>
  </core:macro>

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

---
Import:

<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


Mime
View raw message