SDSU Emerging Technologies
Fall Semester, 2005
TAL
Previous     Lecture Notes Index     Next     
© 2005 All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 22 Sep 2005

 

Doc 7 TAL

Contents

 

ZPT & TAL    

Declaring Variables    

Types of Variables    

Global System Variables    

TAL Commands    

condition    

repeat    

attributes    

omit-tag    

on-error    

 

Copyright ©, All rights reserved. 2005 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml ) license defines the copyright on this document.

 

References

 

TAL Specification 1.4 http://www.zope.org/Wikis/DevSite/Projects/ZPT/TAL%20Specification%201.4

 

TALES Specification 1.3 http://www.zope.org/Wikis/DevSite/Projects/ZPT/TALES%20Specification%201.3

Web Component Development with Zope 3, Philipp von Weitershausen, Springer-Verlag, 2005

This document covers section 7.2 in the above text

 

 

 

 

ZPT & TAL

 

ZPT - Zope Page Template

Imbed code inside of HTML tags

 

TAL - Template Attribute Language

 

Commands

Variable types

Globals

Modifiers

attributes

nothing

context

not

condition

path

default

nocall

content

python

modules

structure

define

string

nothing

| (or)

omit-tag

 

repeat

 

on-error

 

request

 

repeat

 

view

 

replace

     

 

 

Page with TAL

 

<html>

<body>

<div tal:define="message string: Hello World">

    <h1>TAL Example</h1>

    <p tal:replace="message">

        Message here

    </p>

    <p tal:content="message">

        Message here

    </p>

</div>

</body>

</html>

 

Resulting Page

<html>

<body>

<div>

    <h1>TAL Example</h1>

     Hello World

    <p> Hello World</p>

</div>

</body>

</html>

 

 

 

Declaring Variables

Global

 

<html>

<body>

    <p tal:define="global message string: Hi">

        Define here

    </p>

    <p tal:content="message">

        Message here

    </p>

</body>

</html>

 

Globals have to be defined above where they are used

Globals are know in all tags below the definition

 

 

Local

<html>

<body tal:define="local cat string: Meow">

    <p tal:define="message string: Hi ${request/HTTP_USER_AGENT}">

        Message is here <b tal:content="message"></b>

    </p>

    <p>

        Message not known here, but cat is<span tal:replace="cat"/>

    </p>

</body>

</html>

 

Resulting Page

<html>

<body>

    <p>

            Message is here <b> Hi Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412.7 (KHTML, like Gecko) Safari/412.5</b>

    </p>

    <p>

        Message not known here, but cat is Meow

    </p>

</body>

</html>

 

Locals are know only in the tags where they are defined

 

 

Types of Variables

 

  1. a/b/c - path to some object

  1. path - path to some object

  1. string - text

  1. python - python code

  1. nothing - nothing

 

path

<p tal:define="message request/HTTP_USER_AGENT">

<p tal:define="message path:request/HTTP_USER_AGENT">

 

 

python

 

Python expressions only

Only one line allowed for Python expression

 

<html>

<body>

    <p tal:define="message python: 1 + 3">

        Message is here <b tal:content="message"></b>

    </p>

</body>

</html>

 

Resulting Page

<html>

<body>

    <p>

        Message is here <b>4</b>

    </p>

</body>

</html>

 

Global System Variables

 

  1. context

  1. default

  1. modules

  1. nothing

  1. repeat

  1. request

  1. view

 

context

 

The object that the Page Template is to display

Examples later

 

 

default

 

Use the given default value

<p>

        You are using <b tal:content="request/HTTP_USER_AGENT | default">a browser</b>

</p>

 

If user agent is not in request then we get:

 

<p>

        You are using <b>a browser</b>

</p>

 

 

 

Modules

 

Imports Python modules in a Page Template

<html tal:define="random modules/random" >

 

Have not been able to import Book modules

 

nothing

 

a false and empty value

<p tal:condition="nothing">

Will not be printed

</p>

 

repeat

 

used in the repeat command

 

request

 

represents the http request & headers

 

view

 

The view component that the Page Template is part of

More when we cover views

 

TAL Commands

  1. define

  1. condition

  1. repeat

  1. content

  1. replace

  1. attributes

  1. omit-tag

  1. on-error

 

define

define a variable

 

<html tal:define="random modules/random" >

 

 

condition

 

Removes the element if the expression evaluates to true

<html tal:define="random modules/random" >

<body tal:define="small python: random.randrange(10)  < 5" >

    <p tal:condition="small">Small</p>

    <p tal:condition="not: small">Large</p>

</body>

</html>

 

One Possible Result

<html>

<body>

    

    <p>Large</p>

</body>

</html>

 

Second Version

<html tal:define="random modules/random" >

<body

tal:define="test python: random.randrange(10)" >

    <p tal:condition="python: test < 5">Small</p>

    <p tal:condition="python:  not test < 5">Large</p>

</body>

</html>

 

 

repeat

 

Repeats current element for every item in the sequence

 

<html tal:define="list python: ['a', 'b', 'c']" >

<body>

<ul>

    <li tal:repeat="element list">

        <span tal:replace="element">list element</span></li>

</ul>

</body>

</html>

 

Result

<html>

<body>

<ul>

    <li>

        a</li>

    <li>

        b</li>

    <li>

        c</li>

</ul>

</body>

</html>

 

 

 

Repeat & Request - All Http Headers

 

<html>

<body>

<table>

    <tr><td>Header</td><td>Value</td></tr>

    <tr tal:repeat="element request">

        <td tal:content="element"></td>

        <td tal:content="python: request[element]"></td>

    </tr>

</body>

</html>

 

Result

<html>

<body>

<table>

    <tr><td>Header</td><td>Value</td></tr>

    <tr>

        <td>CONNECTION_TYPE</td>

        <td>keep-alive</td>

    </tr>

    <tr>

        <td>SERVER_SOFTWARE</td>

        <td>zope.server.http (HTTP)</td>

    </tr>

<! Stuff removed to save space>    

</table></body>

</html>

 

 

content

 

Fills the element with the return value of the expression

The tag is not removed

replace

Fills the element with the return value of the expression

The tag is not removed

<html>

<body>

<div tal:define="message string: Hello World">

    <h1>TAL Example</h1>

    <p tal:replace="message">

        Message here

    </p>

    <p tal:content="message">

        Message here

    </p>

</div>

</body>

</html>

 

 

attributes

 

Allows you to add an attribute or change the value of an attribute in a tag

<html>

<body tal:define="baseUrl string: http://www.eli.sdsu.edu /" >

<a href="sample.html"

    tal:attributes="href baseUrl">a Link</a>

</body>

</html>

 

Result

<html>

<body>

<a href=" http://www.eli.sdsu.edu /">a Link</a>

</body>

</html>

 

 

omit-tag

 

Omit the tag if the expression is true, but contents

True is default value

 

<html>

<body tal:define="bold python: False">

<div tal:omit-tag="" comment="This tag will be removed">

    <i>...but this text will remain.</i>

</div>

<b tal:omit-tag="not:bold">I may not be bold.</b>

</body>

</html>

 

Result

<html>

<body>

 

    <i>...but this text will remain.</i>

 

I may not be bold.

</body>

</html>

 

 

 

on-error

 

Inserts the return value of an expression if an error occurs in subelement processing

<html>

<body>

<p tal:on-error="string: Error! This paragraph is buggy!">

    My name is <span tal:replace="here/SlimShady" />.

    <br />

    Hi

</p>

</body>

</html>

 

here/SlimShady does not exist in Zope 3

Results in a runtime error

Result

<html>

<body>

<p> Error! This paragraph is buggy!</p>

</body>

</html>

 

 

Structure

 

Expression is not quoted to escape special XML characters

 

<html>

<body tal:define="someHtml string:<b>Be bold</b>" >

<p tal:content="someHtml"></p>

<p tal:content="structure someHtml"></p>

<p tal:content="someHtml"/>

</body>

</html>

 

Result

<html>

<body>

<p>&lt;b&gt;Be bold&lt;/b&gt;</p>

<p><b>Be bold</b></p>

<p>&lt;b&gt;Be bold&lt;/b&gt;</p>

</body>

</html>

Previous     visitors since 22 Sep 2005     Next