Emerging Technologies
Fall Semester, 2005 TAL |
||
---|---|---|
© 2005 All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 22 Sep 2005 |
Doc 7 TAL
Contents
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.
CS683 Fall 2005 | Doc 7, TAL Slide # 2 |
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
CS683 Fall 2005 | Doc 7, TAL Slide # 3 |
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 |
CS683 Fall 2005 | Doc 7, TAL Slide # 4 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 5 |
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
CS683 Fall 2005 | Doc 7, TAL Slide # 6 |
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
CS683 Fall 2005 | Doc 7, TAL Slide # 7 |
a/b/c - path to some object
path - path to some object
string - text
python - python code
nothing - nothing
path
<p tal:define="message request/HTTP_USER_AGENT">
<p tal:define="message path:request/HTTP_USER_AGENT">
CS683 Fall 2005 | Doc 7, TAL Slide # 8 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 9 |
context
default
modules
nothing
repeat
request
view
context
The object that the Page Template is to display
Examples later
CS683 Fall 2005 | Doc 7, TAL Slide # 10 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 11 |
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
CS683 Fall 2005 | Doc 7, TAL Slide # 12 |
request
represents the http request & headers
view
The view component that the Page Template is part of
More when we cover views
CS683 Fall 2005 | Doc 7, TAL Slide # 13 |
define
condition
repeat
content
replace
attributes
omit-tag
on-error
define
define a variable
<html tal:define="random modules/random" >
CS683 Fall 2005 | Doc 7, TAL Slide # 14 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 15 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 16 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 17 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 18 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 19 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 20 |
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>
CS683 Fall 2005 | Doc 7, TAL Slide # 21 |
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><b>Be bold</b></p>
<p><b>Be bold</b></p>
<p><b>Be bold</b></p>
</body>
</html>
Copyright ©, All rights reserved.
2005 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.