Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
Python 如何使用genshi.builder以编程方式构建HTML文档?_Python_Html_Templates_Genshi - Fatal编程技术网

Python 如何使用genshi.builder以编程方式构建HTML文档?

Python 如何使用genshi.builder以编程方式构建HTML文档?,python,html,templates,genshi,Python,Html,Templates,Genshi,我最近发现了genshi.builder模块。这让我想起了Divmod Nevow的Stan模块。如何使用genshi.builder.tag构建具有特定doctype的HTML文档?或者这是一件好事?如果不是,正确的方法是什么?Genshi.builder用于“以编程方式生成标记流”[1]。我相信它的目的是作为模板语言的后端。您可能正在寻找生成整个页面的模板语言 但是,您可以执行以下操作: >>> import genshi.output >>> gensh

我最近发现了genshi.builder模块。这让我想起了Divmod Nevow的Stan模块。如何使用genshi.builder.tag构建具有特定doctype的HTML文档?或者这是一件好事?如果不是,正确的方法是什么?

Genshi.builder用于“以编程方式生成标记流”[1]。我相信它的目的是作为模板语言的后端。您可能正在寻找生成整个页面的模板语言

但是,您可以执行以下操作:

>>> import genshi.output
>>> genshi.output.DocType('html')
('html', '-//W3C//DTD HTML 4.01//EN', 'http://www.w3.org/TR/html4/strict.dtd')
请参见此处的其他doctype:


仅使用
genshi.builder.tag
不可能构建整个页面——您需要对生成的流执行一些操作以插入doctype。此外,生成的代码看起来很可怕。建议使用Genshi的方法是使用单独的模板文件,从中生成流,然后将该流呈现为所需的输出类型

genshi.builder.tag
在需要从Python中生成简单标记时非常有用,例如在构建表单或对输出进行某种逻辑性很强的修改时

有关以下信息,请参阅文档:

如果您确实希望仅使用
builder.tag
生成完整文档,那么这段(完全未经测试的)代码可能是一个很好的起点:

from itertools import chain
from genshi.core import DOCTYPE, Stream
from genshi.output import DocType
from genshi.builder import tag as t

# Build the page using `genshi.builder.tag`
page = t.html (t.head (t.title ("Hello world!")), t.body (t.div ("Body text")))

# Convert the page element into a stream
stream = page.generate ()

# Chain the page stream with a stream containing only an HTML4 doctype declaration
stream = Stream (chain ([(DOCTYPE, DocType.get ('html4'), None)], stream))

# Convert the stream to text using the "html" renderer (could also be xml, xhtml, text, etc)
text = stream.render ('html')

生成的页面中没有空格——看起来很正常,但您将很难阅读源代码,因为它将完全在一行上。实现适当的过滤器以添加空白留给读者作为练习。

有没有一种方法可以使用genshi构建html文档?我在找和斯坦相似的东西。如果这不是一个适当的使用根植,那很好,但我想知道是否是这样。这是有道理的。听起来像是使用ajax或其他工具构建html片段以加载到页面中的东西?即使对于小型ajax页面,我也更喜欢模板,因为它们往往只是简单的“如果登录,显示名称”逻辑和参数替换。tag用于处理一些非常复杂的问题,我不知道如何使用模板很好地编写这些问题。
from itertools import chain
from genshi.core import DOCTYPE, Stream
from genshi.output import DocType
from genshi.builder import tag as t

# Build the page using `genshi.builder.tag`
page = t.html (t.head (t.title ("Hello world!")), t.body (t.div ("Body text")))

# Convert the page element into a stream
stream = page.generate ()

# Chain the page stream with a stream containing only an HTML4 doctype declaration
stream = Stream (chain ([(DOCTYPE, DocType.get ('html4'), None)], stream))

# Convert the stream to text using the "html" renderer (could also be xml, xhtml, text, etc)
text = stream.render ('html')