我正在用web2py的HTML助手制作一个表格。我的代码是基于the example from the web2py book的
>>> table = [['a', 'b'], ['c', 'd']]
>>> print TABLE(TR(*table[0]), TR(*table[1]))
<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>我有一个相当大的表,但这种方法将所有输出放在一大行上。为了HTML的可读性,我希望能有一种在每行之后添加新行的方法。我喜欢HTML助手函数,所以我不喜欢在视图中使用简单的{{for ...}} ... {{pass}}方法。
发布于 2011-05-19 00:19:31
在每一行结束标记后插入一个'\n‘的python代码行应该可以做到这一点。像这样的东西
{{
table = [['a', 'b'], ['c', 'd']]
table_html=XML(TABLE(TR(*table[0]), TR(*table[1])))
table_html=table_html.replace('</tr>','</tr>\n')
response.write(table_html,escape=False)
}}这是在做什么?
它序列化(转换为字符串)表助手,使用python string属性replace()插入换行符,最后使用web2py函数response.write()将修改后的字符串输出到文档。
https://stackoverflow.com/questions/6047474
复制相似问题