Genshin:for循环插入换行符
Genshi: for loop inserts line breaks
源代码:我有如下程序
import genshi
from genshi.template import MarkupTemplate
html = '''
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/">
<head>
</head>
<body>
<py:for each="i in range(3)">
<py:choose>
<del py:when="i == 1">
${i}
</del>
<py:otherwise>
${i}
</py:otherwise>
</py:choose>
</py:for>
</body>
</html>
'''
template = MarkupTemplate(html)
stream = template.generate()
html = stream.render('html')
print(html)
预期输出: 数字连续打印,中间没有空格(最重要的是没有换行符)。
<html>
<head>
</head>
<body>
0<del>1</del>2
</body>
</html>
实际输出: 输出如下:
<html>
<head>
</head>
<body>
0
<del>1</del>
2
</body>
</html>
问题:如何消除换行符?我可以通过从最后的 HTML 中剥离它来处理前导空格,但我不知道如何摆脱换行符。我需要将 for 循环的内容显示为单个连续的 "word"(例如 012
而不是 0 \n 1 \n 2
)。
我试过的:
- 正在阅读 Genshi 文档。
- 正在搜索 Whosebug
- 正在搜索Google
使用 <?python ...code... ?>
代码块。这不起作用,因为 <del>
标记中的插入符号被转义并显示。
<?python
def numbers():
n = ''
for i in range(3):
if i == 1:
n += '<del>{i}</del>'.format(i=i)
else:
n += str(i)
return n
?>
${numbers()}
产生0<del>1</del>2
我也试过这个,但使用 genshi.builder.Element('del')
代替。结果是一样的,我能够最终确定由 numbers()
编辑的字符串 return 在 return 发生后被转义。
其他一堆暂时想不起来了
不太理想,但我终于找到了可以接受的解决方案。诀窍是将给定标签的结束插入符放在下一行的下一个标签的开始插入符之前。
<body>
<py:for each="i in range(3)"
><py:choose
><del py:when="i == 1">${i}</del
><py:otherwise>${i}</py:otherwise
></py:choose
</py:for>
</body>
来源:https://css-tricks.com/fighting-the-space-between-inline-block-elements/
如果有人有更好的方法,我很想听听。
源代码:我有如下程序
import genshi
from genshi.template import MarkupTemplate
html = '''
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/">
<head>
</head>
<body>
<py:for each="i in range(3)">
<py:choose>
<del py:when="i == 1">
${i}
</del>
<py:otherwise>
${i}
</py:otherwise>
</py:choose>
</py:for>
</body>
</html>
'''
template = MarkupTemplate(html)
stream = template.generate()
html = stream.render('html')
print(html)
预期输出: 数字连续打印,中间没有空格(最重要的是没有换行符)。
<html>
<head>
</head>
<body>
0<del>1</del>2
</body>
</html>
实际输出: 输出如下:
<html>
<head>
</head>
<body>
0
<del>1</del>
2
</body>
</html>
问题:如何消除换行符?我可以通过从最后的 HTML 中剥离它来处理前导空格,但我不知道如何摆脱换行符。我需要将 for 循环的内容显示为单个连续的 "word"(例如 012
而不是 0 \n 1 \n 2
)。
我试过的:
- 正在阅读 Genshi 文档。
- 正在搜索 Whosebug
- 正在搜索Google
使用
<?python ...code... ?>
代码块。这不起作用,因为<del>
标记中的插入符号被转义并显示。<?python def numbers(): n = '' for i in range(3): if i == 1: n += '<del>{i}</del>'.format(i=i) else: n += str(i) return n ?> ${numbers()}
产生
0<del>1</del>2
我也试过这个,但使用genshi.builder.Element('del')
代替。结果是一样的,我能够最终确定由numbers()
编辑的字符串 return 在 return 发生后被转义。其他一堆暂时想不起来了
不太理想,但我终于找到了可以接受的解决方案。诀窍是将给定标签的结束插入符放在下一行的下一个标签的开始插入符之前。
<body>
<py:for each="i in range(3)"
><py:choose
><del py:when="i == 1">${i}</del
><py:otherwise>${i}</py:otherwise
></py:choose
</py:for>
</body>
来源:https://css-tricks.com/fighting-the-space-between-inline-block-elements/
如果有人有更好的方法,我很想听听。