Examples¶
Basic usage¶
- As simple as it gets:
- Create a context
- Compile a template
- Expand the template
Code¶
from simpletal import simpleTAL, simpleTALES
import sys
# Creat the context that is used by the template
context = simpleTALES.Context(allowPythonPath=1)
# Add a string to the context under the variable title
context.addGlobal("title", "Colours of the rainbow")
# A list of strings
colours = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
# Add the list to the context under the variable rainbow
context.addGlobal("rainbow", colours)
# Open the template file
templateFile = open("basic.html", 'r')
# Compile a template
template = simpleTAL.compileHTMLTemplate(templateFile)
# Close the template file
templateFile.close()
# Expand the template as HTML using this context
template.expand(context, sys.stdout, outputEncoding="utf-8")
basic.html¶
<html>
<body>
<h1 tal:content="title">The title</h1>
<ul tal:repeat="colour rainbow">
<li tal:content="colour">Colour of the rainbow</li>
One Two
<p tal:content="python: str (colour) + ' is the colour'"></p>
</ul>
</body>
</html>
CGI example¶
- A demonstration of how TAL/TALES can be used from a cgi program. Quick instructions:
- Copy this file and the two templates (“fields.html” and “results.html”) to the cgi-bin directory on your webserver
- Ensure that simpleTAL, simpleTALES and DummyLogger are installed in your site-packages directory
- Go to http://servername/cgi-bin/simple-cgi.py
Code¶
from simpletal import simpleTAL, simpleTALES
import cgi
import sys
class ExampleCGI:
def __init__(self):
self.missingFields = {}
self.fieldValues = {}
self.form = cgi.FieldStorage()
self.formValid = 1
self.context = simpleTALES.Context()
def buildContext(self, title):
self.context.addGlobal("missingFields", self.missingFields)
self.context.addGlobal("fieldValues", self.fieldValues)
self.context.addGlobal("title", title)
def getValue(self, name, mandatory=1):
if (self.form.has_key(name)):
self.fieldValues[name] = self.form[name].value
elif (mandatory):
self.missingFields[name] = 1
self.formValid = 0
def main(self):
if (self.form.has_key("submit")):
# Recieved the posting, get the name, occupation, and (optional)
# age
self.getValue("username")
self.getValue("occupation")
self.getValue("age", mandatory=0)
if (self.formValid):
# Valid form, show the results
self.buildContext("Valid Results")
self.expandTemplate("results.html")
else:
self.buildContext("Missing fields")
self.expandTemplate("fields.html")
else:
self.buildContext("Enter data")
self.expandTemplate("fields.html")
def expandTemplate(self, templateName):
# Print out the headers
sys.stdout.write("Content-Type: text/html\n") # HTML is following
sys.stdout.write("\n") # blank line, end of headers
# Expand the template and print it out
templateFile = open(templateName, 'r')
template = simpleTAL.compileHTMLTemplate(templateFile)
# Close the template file
templateFile.close()
# Expand the template as HTML using this context
template.expand(self.context, sys.stdout)
sys.exit(0)
# Entry point for the cgi
cgiInstance = ExampleCGI()
cgiInstance.main()
fields.html¶
<!-- Example TAL HTML Template - this is used to gather data -->
<html>
<head>
<title tal:content="title">Title</title>
</head>
<body>
<h1 tal:content="title">Title</h1>
<form action="simple-cgi.py">
<div>
Name: <input name="username" tal:attributes="value fieldValues/username"></input>
<b tal:condition="missingFields/username">(Please fill in)</b><br>
Occupation: <input name="occupation" tal:attributes="value fieldValues/occupation"></input>
<b tal:condition="missingFields/occupation">(Please fill in)</b><br>
Age: <input name="age" tal:attributes="value fieldValues/age"></input><br>
</div>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
results.html¶
<!-- Example TAL HTML Template - this is used to show results -->
<html>
<head>
<title tal:content="title">Title</title>
</head>
<body>
<h1 tal:content="title">Title</h1>
<div>
Name: <b tal:content="fieldValues/username">Your name</b><br>
Occupation: <b tal:content="fieldValues/occupation">Your occupation</b><br>
<div tal:condition="fieldValues/age">Age:
<b tal:content="fieldValues/age">Your age</b></div><br>
</div>
</body>
</html>
METAL example¶
An example of how to use METAL.
Code¶
from simpletal import simpleTAL, simpleTALES
import sys
# Creat the context that is used by the template
context = simpleTALES.Context()
# Add a string to the context under the variable title
context.addGlobal("title", "Simple METAL Example")
# Compile the macro pages
templateFile = open("macro.html", 'r')
macros = simpleTAL.compileHTMLTemplate(templateFile)
templateFile.close()
# Add the macros page to the Context
context.addGlobal("sitemacros", macros)
# Now compile the page which will use the macros
templateFile = open("page.html", 'r')
page = simpleTAL.compileHTMLTemplate(templateFile)
templateFile.close()
# Expand the page using this context
page.expand(context, sys.stdout)
macro.html¶
<html>
<body>
<h1 tal:content="title">The title</h1>
<p metal:define-macro="notice">
This macro is brought to you by <b metal:define-slot="source">the colour
blue</b>.
Now you know.
</p>
</body>
</html>
page.html¶
<html>
<body>
<h1 tal:content="title">The title</h1>
<div metal:use-macro="sitemacros/macros/notice">
<i metal:fill-slot="source">SimpleTAL!</i>
</div>
</body>
</html>
Structure example¶
This shows how to include structure into a template, and how multiple templates can be embedded within each other.
Code¶
from simpletal import simpleTAL, simpleTALES
import sys
# Create the context that is used by the template
context = simpleTALES.Context()
context.addGlobal("title", "Hello World")
context.addGlobal("author", "Colin Stewart")
# A list that contains a dictionary
chapters = [{"heading": "Introduction", "text": "Some <b>text</b> here"}, {"heading": "Details", "text": "Notice tags are preserved."}
]
advancedText = 'Structured text can contain other templates like this - written by <b tal:replace="author">Me</b>'
chapters.append(
{"heading": "Advanced", "text": simpleTAL.compileHTMLTemplate(advancedText)})
context.addGlobal("doc", chapters)
templateFile = open("structure.html", 'r')
template = simpleTAL.compileHTMLTemplate(templateFile)
templateFile.close()
template.expand(context, sys.stdout)
structure.html¶
<html>
<body>
<h1 tal:content="title">The title</h1>
<div tal:repeat="chapters doc">
<h2 tal:content="chapters/heading">Chapter Heading</h2>
<p tal:content="structure chapters/text">Text</p>
</div>
</body>
</html>