Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对大型HTML文件使用Python CGI_Python_Html_Web_Cgi - Fatal编程技术网

对大型HTML文件使用Python CGI

对大型HTML文件使用Python CGI,python,html,web,cgi,Python,Html,Web,Cgi,我有一个很大的html文件名为exercise.html,我需要通过Python CGI生成一个som文件。 我想问你打印这个HTML的最佳方式是什么 我知道可以通过打印方法使用格式方法%s、%I等: print '''<html> <head><title>My first Python CGI app</title></head> <body> <p>Hello, 'world'!</p> .

我有一个很大的html文件名为exercise.html,我需要通过Python CGI生成一个som文件。 我想问你打印这个HTML的最佳方式是什么

我知道可以通过打印方法使用格式方法%s、%I等:

print '''<html>
<head><title>My first Python CGI app</title></head>
<body> 
<p>Hello, 'world'!</p>
.
.
<div>%s</div>
.
.
</body>
</html>''' % generated_text
打印“”“
我的第一个Python CGI应用程序
你好,世界

. . % . . ''已生成\u文本

但是这个HTML很大,所以这是唯一的一个解决方案吗?

你应该考虑使用模板语言,比如. 下面是一个简单的例子,直接来自上面的链接:

>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe')
通常,尽管您将模板保存在文件中,然后加载/处理它们:

from jinja2 import Environment, PackageLoader
# The env object below finds templates that are lcated in the `templates`
# directory of your `yourapplication` package.
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
template = env.get_template('mytemplate.html')
print template.render(the='variables', go='here')
如上所示,模板允许您将
变量
放入模板中。将文本放置在{{}中使其成为模板变量。呈现模板时,请使用关键字参数传入变量值。例如,下面的模板有一个名称变量,我们通过template.render传递该变量

这是我的{{name}

也考虑()。值得注意的是,瓶子.py支持mako、jinja2和cheetah模板

%表示python代码和{{var}}是替换变量

HTML:

template.render(name='Jaime')
<ul>
  % for item in basket:
  <li>{{item}}</li>
  % end
</ul>
with open('index.html', 'r') as htmlFile:
    return bottle.template(htmlFile.read(), basket=['item1', 'item2'])