[ZPT] Usage example for standalone PageTemplates?

Hamish Lawson hbl@st-andrews.ac.uk
Fri, 30 Mar 2001 17:49:48 +0100


Guido wrote:

> You can't reasily use the PageTemplates code, its execution engine is
> too closely tied to Zope.  But TAL runs completely stand-alone.  The
> best example is the TAL/driver.py script.

I've adapted the following demo program from driver.py. However I can't see
from the TAL source code how to pass the data that I want to be substituted
into the template; so in the program below I've resorted to hard-coding the
data in the template itself (just as the input file used by driver.py seems
to do). But how do I pass the data from 'record' into the template instead?

Thanks,
Hamish

---

from TAL.HTMLTALParser import HTMLTALParser
from TAL.TALInterpreter import TALInterpreter
from TAL.DummyEngine import DummyEngine
import cStringIO

record = {'name': 'Hamish', 'age': 36}

template = """\
<body tal:define="global name str:Fred; global age str:52">
<p>My name is <span tal:replace="name">Joe</span>
and I am aged <span tal:replace="age">40</span>.</p>
</body>
"""

parser = HTMLTALParser()
parser.parseString(template)
program, macros = parser.getCode()
engine = DummyEngine(macros)
outfile = cStringIO.StringIO()
interp = TALInterpreter(program, macros, engine, outfile)
interp()
print outfile.getvalue()