Some examples demonstrating how to use SimpleTAL.

SimpleTAL Examples.

ExpenseClam
Easy-to-use business expense tracker for Android.

ExpenseClam for Android

The download includes a few examples of how to use the library, including from a CGI and a stand alone script.  Here are a couple of Python 3 examples showing how to use SimpleTAL - only a small selection of TAL's abilities are shown here.

 

Basic Example

Here's a snippet of code that shows how easy the library is to use:


import sys
from simpletal import simpleTAL, simpleTALES

# Create the context that is used by the template
context = simpleTALES.Context()
context.addGlobal ("title", "Hello World")

templateFile = open ("simple.html", 'rt', encoding = 'utf-8')
template = simpleTAL.compileHTMLTemplate (templateFile)
templateFile.close()
template.expand (context, sys.stdout)

The template could of course be any HTML, for example:


<html>
<h1 tal:content="title">The title goes here</h1>
</html>

Structure Example

A more complicated example, showing the use of lists and dictionaries in a context.  The template given here shows how the "structure" keyword can be used, with tal:content and tal:replace, to include tags as actual markup rather than escaping it.  This example also demonstrates how templates can be passed into the context, allowing multiple templates to be embedded within each other.

(This code is now included in the examples directory of the download)


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", 'rt', encoding = 'utf-8')
template = simpleTAL.compileHTMLTemplate (templateFile)

templateFile.close()
template.expand (context, sys.stdout)

The corresponding HTML template:


<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>

METAL Example

METAL is a macro language that consists of four commands.  These commands, all shown in this example, allow a macro to be declared with customisation points, and then utilised with customisation optionally performed.  This code is included in the examples directory of the download.

Firstly lets look at the definition of a macro with one customisation point:


<html>
  <body>
    <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>

The macro in this example is called "notice", and has one customisation point called "source".  If the macro is used without the "source" slot being filled the default markup given in the macro will be used.

An example of a page that might use this macro is:


<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>

The metal:use-macro command takes one parameter, the location of the macro to use.  This is in the form of a TALES expression, the same as is used for TAL, and so will be resolved in the same way.  If a template has any macros defined in it they will be made available in the context of that template under the name 'macros' as seen here.

The code which drives the template expansion is:

from simpletal import simpleTAL, simpleTALES
import sys

context = simpleTALES.Context()

context.addGlobal ("title", "Simple METAL Example")
templateFile = open ("macro.html", 'rt', encoding="utf-8")
macros = simpleTAL.compileHTMLTemplate (templateFile)
templateFile.close()

context.addGlobal ("sitemacros", macros)

templateFile = open ("page.html", 'rt', encoding = "utf-8")
page = simpleTAL.compileHTMLTemplate (templateFile)
templateFile.close()

page.expand (context, sys.stdout)

Back to SimpleTAL

%nbsp;

Last Modified: Thu, 13 Aug 2009 19:06:46 BST

Made with PubTal 3.5

Copyright 2013 Colin Stewart

Email: colin at owlfish.com