[Xotcl] Illusion templates

Kristoffer Lawson setok at fishpool.com
Wed Feb 6 17:14:13 CET 2008


On 6 Feb 2008, at 17:18, Jade Rubick wrote:

> Kristoffer:
>
> Can you post some more information about how the API looks and works?
> Your blog post doesn't have much information on how it works or looks.

Sure. Well, for a start the package itself contains a couple of  
really simple examples, but I'll copy the simplest one here.

Here is what the template looks like:

<!-- ILL-TMPL SRC example.tcl -->
<html>
   <body>
     Hello world. Let's try some magic:
     <p>
       <ul>
         <!-- ILL-OB ColourList START -->
         <li>%%colour%%</li>
         <!-- ILL-OB ColourList END -->
       </ul>
     </p>
     End of magic
   </body>
</html>

This template is for creating a list of colours. example.tcl, as  
specified on the first line, contains the logic for the content of  
objects and their amount. It is evaluated along with the template.  
This is what it looks like:

Class ColourList -parameter {
     colour
}

foreach colour {red green blue} {
     set ob [ColourList new -colour $colour]
     ColourList lappend obOrder $ob
}

So basically it creates a single ColourList object for each colour we  
want. It also appends them to obOrder to display them in the right  
order (this phase is optional). The template uses the 'colour' method  
to receive values, which is here implemented with -parameter, but  
could easily be a method with some more logic instead.

To get the result we simply do:

set data [illusion::processTemplate example]

Here's the result:

<html>
   <body>
     Hello world. Let's try some magic:
     <p>
       <ul>

         <li>red</li>

         <li>green</li>

         <li>blue</li>

       </ul>
     </p>
     End of magic
   </body>
</html>

I also have added a feature to include other template files. I've  
kept it simple on purpose as I don't think a template file should be  
complex. The complexity should be in the Tcl. I do think this could  
lead to quite a versatile solution, though.

            / http://www.scred.com/
            / http://www.fishpool.com/~setok/




More information about the Xotcl mailing list