使用 TAL、Chameleon 和 Pyramid 重复元素时遇到问题
我真的很难让 TAL 和 Chameleon/Pyramid 好好相处。 。 。
我在 Pyramid 中有一个视图,例如返回以下内容:
def view(request):
return {'results' : [ {'name':'alice', 'value':22},
{'name':'bob', 'value':11},
{'name':'charlie', 'value':33} ] }
我有一个模板,其中包含以下 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>
我希望获得以下内容的 HTML 输出:
<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>
但是,当我尝试运行此命令时,Pyramid 会抛出错误:
raise ParseError("Unexpected end tag.", token)
ParseError: Unexpected end tag.
- String: "</tr>"
我觉得这很奇怪。我删除了结束 <\tr>
,然后收到以下错误:
NameError: row
- Expression: "python:row['name']"
我真的陷入困境,任何帮助将不胜感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
诡异的。我认为这应该有效。
不管怎样,你在这里使用
tal:replace
是错误的,因为它会替换整个标签。您想要的是将标签的内容替换为tal:content
:python:
在 Chameleon 中也是多余的。然后,您还可以在模板中使用属性访问而不是字典访问:最后,您可以使用
${}
替换语法而不是tal:content
。这会给你一个更紧凑的拼写:也许,在这样做的同时,你会解决你的实际问题。 ;-)
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 withtal:content
:The
python:
is also superfluous in Chameleon. Then, you can also use attribute access instead of dict access in templates:Lastly, you can use the
${}
substitution syntax instead oftal:content
. That'll give you a much more compact spelling:And maybe, while doing this, you'll solve your actual problem on the way. ;-)