[Zope] Page Template TAL tags: no HTML tag required

Jon Whitener wmmail@twmi.rr.com
Wed, 15 Jan 2003 13:11:14 -0500


Here's something useful about the Template Attribute Language I didn't know:  TAL statements DO NOT NEED to be inside an HTML tag.  They can be inserted in Zope Page Templates as their own, separate tag.  Look at this ZPT code:

  <tal:spam define="name string:Hipple-Dee-Dipple">
    Why, hello there, <tal:fish-slap replace="name" />!  Nice to see you.
  </tal:spam>

No HTML tags in there!  The spam tag has an end tag, the fish-slap tag needs none.

Steve Alexander explains:
>Because of the way TAL uses XML namespaces, you can call an element "tal:whateveryoulike". All of its attributes will be in the 'tal:' namespace by default. So, you could say:
>
>  <tal:foo tal:replace="name" />
>
>Which is equivalent to
>
>  <tal:bar tal:replace="name" />
>
>Which is equivalent to
>
>  <tal:baz replace="name" />

Shane Hathaway points out:
>Attributes of a TAL tag default to the "tal:" namespace, so you don't have to write "tal:" more than once per tag.  (In fact, if you use an xmlns attribute, you don't have to use a "tal:" prefix at all, but then you have to prefix everything else.)

An example:

  <tal:mytag define="msg string:Good day!" condition="msg" content="msg" />


Godefroid Chapelle points to the documentation of this TAL feature:
>http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/AdvZPT.stx#3-45


Steve Alexander shows how TAL syntax is similar to that of DTML (and ASP):
>DTML: Hello <b><dtml-var name></b>, how are you?
>ZPT:  Hello <b><tal:tag replace="name"/></b>, how are you?
>
>In ZPT, one of the constraints is to use valid XML. <dtml-var name> is not valid xml, because "name" should be in quotes, and it should have an attribute name. Actually, <dtml-var name> is a short-hand for <dtml-var name="name">. So, without cheesy short-cuts:
>
>DTML: Hello <b><dtml-var name="name"></b>, how are you?
>ZPT:  Hello <b><tal:tag replace="name"/></b>, how are you?
>
>There's not so much difference now.


Now I will stop using otherwise-useless <span> tags just to host my TAL statements!

Jon Whitener
Detroit Michigan USA