[Zope] Using TAL outside Zope with path expressions

Rodrigo Dias Arruda Senra rodsenra at gpr.com.br
Thu Jul 22 17:26:19 EDT 2004


 Hi,

 I'm trying to use TAL (from Zope 2.7.0) outside the
 Zope context (== standalone program).

 I have cooked a small test case to illustrate
 where I got stuck. The problems are the following:

 1) I could not use path expressions inside TAL repeat loops,
    something like "tal:content='itervar/someattr'" fails.
    - Apparently DummyEngine does not parses nor handles
      path expression inside evaluatePathOrVar(expr). It just
      checks if it belongs to self.locals or sefl.globals
    How does it work inside Zope ? Does it use a different Engine ?
    I failed to locate this in the Zope code, any pointers ?

 2) I could not find how Zope handles 'repeat/itervar/odd' and
    simmilar constructions upon the repeat var. This also fails
    when I use TAL outside Zope.

 3) I found some 'z:...' instead of 'tal:...' constructions inside
    the test cases. They are equivalent ? Is this documented somewhere ?  
 
 TIA,

 Rod Senra
 rodsenra at gpr.com.br

<code>
from TAL.HTMLTALParser import HTMLTALParser
from TAL.TALInterpreter import TALInterpreter
from TAL.TALGenerator import TALGenerator
from TAL.DummyEngine import DummyEngine


context = {
 'some_list':[{'href':'http://localhost','content':'bar1'},
              {'href':'http://localhost','content':'bar2'}]
}

# This template works fine!
templateOk = """"
<html><body><ul>
 <li tal:repeat="foo some_list">
    <a tal:attributes="href python:foo['href']" 
       tal:content="python:foo['content']">
    </a>
 </li>           
</ul></body></html>
"""

# This template below fails
templateBug = """"
<html><body><ul>
 <li tal:repeat="foo some_list">
    <a tal:attributes="href foo/href" 
       tal:content="foo/content">
    </a>
 </li>           
</ul></body></html>
"""

def generateFile(template,context={},macros={}):
    eng = DummyEngine(macros)
    eng.locals.update(context)
    gen = TALGenerator(expressionCompiler=eng)
    parser = HTMLTALParser(gen)
    parser.parseString(template)
    program, macros = parser.getCode()
    stream = StringIO()
    interp = TALInterpreter(program, macros, eng, stream)
    interp()
    return stream.getvalue().strip()
    
    
print generateFile(templateOk,context)
print generateFile(templateBug,context)
</code>
---------------------------------------------------------------
<output>
<html><body><ul>
 <li>
    <a href="http://localhost">bar1</a>
 </li>
 <li>
    <a href="http://localhost">bar2</a>
 </li>
</ul></body></html>
...
TAL.TALDefs.TALESError: unknown variable: 'foo/href'
</output>



More information about the Zope mailing list