<h1>Hello wolrd</h1>
<div>This is me</div>
我希望在关闭正文标记之前将其附加到我的索引文件中。就像这样:
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<script type="text/ng-template" id="templates/template.html">
<h1>Hello wolrd</h1>
<div>This is me</div>
</script>
</body>
</html>
我到目前为止已经阅读了文件并附加到它的末尾,但我还没有将脚本标记添加到我正在阅读的文件中并附加到我文件的正确位置。这就是我目前的情况:
#!/usr/bin/env python
import fileinput
to_readfile=open('index.html', "r")
try:
reading_file=to_readfile.read()
writefile=open('index2.html','a')
try:
writefile.write("\n")
writefile.write(reading_file)
finally:
writefile.close()
finally:
to_readfile.close()
非常感谢任何帮助。谢谢!
答案 0 :(得分:1)
最简单的方法是在布局模板中添加占位符,然后在处理占位符的布局搜索时将其替换为其他模板的内容。
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<script type="text/ng-template" id="templates/template.html">
{{content}}
</script>
</body>
</html>
...
..
.
layout = open('layout.html', "r")
layout_contents = layout.read()
partial=open('partial_file.html','r')
result = layout_contents.replace("{{content}}", partial)
writefile = open("file_to_write.html", "w")
writefile.write("\n")
writefile.write(result)
.
..
....
您还可以使用更广泛的解决方案,例如jinja http://jinja.pocoo.org/docs/templates/#template-inheritance使用的解决方案。