SÜM*_*ÇAK 7 html python python-2.7
这是我在html模板中替换变量的方法
a1 = '<html><title>{0}</title><body>{1}</body></html>'
vars = ['test', 'test']
output = a1.format(*vars)
Run Code Online (Sandbox Code Playgroud)
问题是..当html中有css时,这与css标签冲突.
因为css包含:
{}'s
Run Code Online (Sandbox Code Playgroud)
替代方法。
由于您混合了 HTML 和 Python 代码,我建议通过切换到普通模板引擎来分离逻辑部分和表示部分。
使用示例mako。
创建一个模板 HTML 文件,留下适当的“占位符”:
<html>
<head>
<title>${title}</title>
</head>
<body>
<p>${text}</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
然后,在 python 代码中,渲染为占位符提供值的模板:
from mako.template import Template
t = Template(filename='index.html')
print t.render(title='My Title', text='My Text')
Run Code Online (Sandbox Code Playgroud)
这将打印:
<html>
<head>
<title>My Title</title>
</head>
<body>
<p>My Text</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1554 次 |
| 最近记录: |