1

I'm really struggling to get TAL and Chameleon/Pyramid to play nice. . .

I have a view in Pyramid that returns, for example, the following:

def view(request):
    return {'results' : [ {'name':'alice', 'value':22}, 
                          {'name':'bob', 'value':11},
                          {'name':'charlie', 'value':33} ] }

I have a template that contains the following bit of HTML:

<!DOCTYPE html>
<html>
<head></head>
<body>
<table>
    <thead>
        <tr>
            <td>Keyword</td>
            <td class="center">Mean Position</td>
        </tr>
    </thead>
    <tbody>
        <tr tal:repeat"row results">
            <td>${row.name}</td>
            <td>${row.value}</td>
        </tr>
    </tbody>
</table>
</body>
</html>

I'm hoping for HTML output of the following:

<table>
    <thead>
    <tr class="odd">
        <td>Name</td>
        <td class="centre">Age</td>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>alice</td>
            <td>22</td>
        </tr>
        <tr>
            <td>bob</td>
            <td>11</td>
        </tr>            
        <tr>
            <td>charlie</td>
            <td>33</td>
        </tr>
    </tbody>
</table>

However, Pyramid throws an error when I try to run this:

raise ParseError("Unexpected end tag.", token)
ParseError: Unexpected end tag.
- String:   "</tr>"

which I thought was rather weird. I removed the closing <\tr>, and then get the following error:

NameError: row
- Expression: "python:row['name']"

I'm really stuck on this and any help would be appreciated!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Edwardr
  • 2,906
  • 3
  • 27
  • 30

1 Answers1

5

Weird. I think this should work.

Anyway, your use of tal:replace here is wrong as it's gonna replace the whole tag. What you want is to replace the contents of the tag with tal:content:

<tr tal:repeat="row results">
    <td tal:content="python:row['name']">the name</td>
    <td tal:content="python:row['value']">1</td>
</tr>

The python: is also superfluous in Chameleon. Then, you can also use attribute access instead of dict access in templates:

<tr tal:repeat="row results">
    <td tal:content="row.name">the name</td>
    <td tal:content="row.value">1</td>
</tr>

Lastly, you can use the ${} substitution syntax instead of tal:content. That'll give you a much more compact spelling:

<tr tal:repeat="row results">
    <td>${row.name}</td>
    <td>${row.value}</td>
</tr>

And maybe, while doing this, you'll solve your actual problem on the way. ;-)

Daniel Nouri
  • 1,264
  • 8
  • 9
  • Thanks for the helpful answer. However, I still am getting the original `` tag error. I've updated the question to show my full template. Is there anything stupid I've missed? – Edwardr Nov 02 '11 at 15:04
  • 1
    I think I found it. It should be `tal:repeat="row results"` not `tal:repeat"row results"`. (The equals sign = is missing.) – Daniel Nouri Nov 04 '11 at 18:21