meld3
Overview
meld3 is an HTML/XML templating system for Python which keeps
template markup and dynamic rendering logic separate from one
another. See http://www.entrian.com/PyMeld for a treatise on the
benefits of this pattern.
meld3 can deal with HTML or XML/XHTML input and can output
well-formed HTML or XML/XHTML.
meld3 is a variation of Paul Winkler's Meld2, which is itself a
variation of Richie Hindle's PyMeld.
meld3 uses Frederik Lundh's ElementTree library.
Requirements
On Python 3, meld3 requires Python 3.2 or later.
On Python 2, meld3 requires Python 2.5 or later.
Installation
Run 'python setup.py install'.
Differences from PyMeld
- Templates created for use under PyMeld will not work under meld3
due to differences in meld tag identification (meld3's id
attributes are in a nondefault XML namespace, PyMeld's are not).
Rationale: it should be possible to look at a template and have a
good shot at figuring out which pieces of it might get replaced
with dynamic content. XML ids are required for other things like
CSS styles, so you can't assume if you see an XML id in a template
that it was put in there to be a meld identifier. In the worst
case scenario, if XML ids were used instead of namespaced id's,
and an unused id was present in the source document, the designer
would leave it in there even if he wasn't using it because he
would think it was a meld id and the programmer would leave it in
there even if he wasn't using it because he would think it was
being used by the designer's stylesheets. Menawhile, nobody's
actually using it and it's just cluttering up the template. Also,
having a separate namespace helps the programmer not stomp on the
designer by changing identifiers (or needing to grep stylesheets),
and lets them avoid fighting over what to call elements.
- The "id" attribute used to mark up is in the a separate namespace
(aka. xmlns="http://www.plope.com/software/meld3"). So instead of
marking up a tag like this: '<div id="thediv"></div>', meld3
requires that you qualify the "id" attribute with a "meld"
namespace element, like this: '<div meld:id="thediv"></div>'. As
per the XML namespace specification, the "meld" name is completely
optional, and must only represent the
"http://www.plope.com/software/meld3" namespace identifier, so
'<div xmlns:foo="http://www.plope.com/software/meld3"
foo:id="thediv"/>' is just as valid as as '<div
meld:id="thediv"/>'
- Output documents by default do not include any meld3 namespace id
attributes. If you wish to preserve meld3 ids (for instance, in
order to do pipelining of meld3 templates), you can preserve meld
ids by passing a "pipeline" option to a "write" function
(e.g. write_xml, wwrite_xhtml).
- Output can be performed in "XML mode", "XHTML mode" and "HTML
mode". HTML output follows recommendations for HTML 4.01, while
XML and XHTML output outputs valid XML If you create an empty
textarea element and output it in XML and XHTML mode the output
will be rendered <'textarea/>'. In HTML mode, it will be rendered
as '<textarea></textarea>'. In HTML mode, various other tags like
'img' aren't "balanced" with an ending tag, and so forth. You can
decide how you wish to render your templates by passing an 'html'
flag to the meld 'writer'.
- meld3 elements are instances of ElementTree elements and support
the "ElementTree _ElementInterface
API":http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm#elementtree.ElementTree._ElementInterface-class)
instead of the PyMeld node API. The ElementTree _ElementInterface
API has been extended by meld3 to perform various functions
specific to meld3.
- meld3 elements do not support the __mod__ method with a sequence
argument; they do support the __mod__ method with a dictionary
argument, however.
- meld3 elements support various ZPT-alike methods like "repeat",
"content", "attributes", and "replace" that are meant to work like
their ZPT counterparts.
Examples
A valid example meld3 template is as
follows::
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:meld="http://www.plope.com/software/meld3"
xmlns:bar="http://foo/bar">
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" />
<title meld:id="title">This is the title</title>
</head>
<body>
<div/> <!-- empty tag -->
<div meld:id="content_well">
<form meld:id="form1" action="." method="POST">
<table border="0" meld:id="table1">
<tbody meld:id="tbody">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<tr meld:id="tr" class="foo">
<td meld:id="td1">Name</td>
<td meld:id="td2">Description</td>
</tr>
</tbody>
</table>
<input type="submit" name="next" value=" Next "/>
</form>
</div>
</body>
</html>
Note that the script contains no logic, only "meld:id" identifiers.
All "meld:id" identifiers in a single document must be unique for a
meld template to be parseable.
A script which parses the above template and does some
transformations is below. Consider the variable "xml" below bound
to a string representing the XHTML above::
from meld3 import parse_xmlstring
from meld3 import parse_htmlstring
from StringIO import StringIO
import sys
root = parse_xmlstring(xml)
root.findmeld('title').content('My document')
root.findmeld('form1').attributes(action='./handler')
data = (
{'name':'Boys',
'description':'Ugly'},
{'name':'Girls',
'description':'Pretty'},
)
iterator = root.findmeld('tr').repeat(data)
for element, item in iterator:
element.findmeld('td1').content(item['name'])
element.findmeld('td2').content(item['description'])
You used the "parse_xmlstring" function to transform the XML into a
tree of nodes above. This was possible because the input was
well-formed XML. If it had not been, you would have needed to use
the "parse_htmlstring" function instead.
To output the result of the transformations to stdout as XML, we use
the 'write' method of any element. Below, we use the root element
(consider it bound to the value of "root" in the above script)::
import sys
root.write_xml(sys.stdout)
...
<?xml version="1.0"?>
<html:html xmlns:html="http://www.w3.org/1999/xhtml">
<html:head>
<html:meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" />
<html:title>My document</html:title>
</html:head>
<html:body>
<html:div /> <!-- empty tag -->
<html:div>
<html:form action="./handler" method="POST">
<html:table border="0">
<html:tbody>
<html:tr>
<html:th>Name</html:th>
<html:th>Description</html:th>
</html:tr>
<html:tr class="foo">
<html:td>Boys</html:td>
<html:td>Ugly</html:td>
</html:tr>
<html:tr class="foo">
<html:td>Girls</html:td>
<html:td>Pretty</html:td>
</html:tr>
</html:tbody>
</html:table>
<html:input name="next" type="submit" value=" Next " />
</html:form>
</html:div>
</html:body>
</html:html>
We can also serialize our element tree as well-formed XHTML, which
is largely like rendering to XML except it by default doesn't emit
the XML declarati
没有合适的资源?快使用搜索试试~ 我知道了~
meld3-1.0.2.tar.gz
需积分: 50 36 下载量 23 浏览量
2017-11-28
09:32:16
上传
评论
收藏 36KB GZ 举报
温馨提示
共18个文件
txt:9个
py:5个
pkg-info:2个
使用easy_install 无法联网安装supervisor,手动安装supervisor时需要的组件
资源推荐
资源详情
资源评论
收起资源包目录
meld3-1.0.2.tar.gz (18个子文件)
meld3-1.0.2
MANIFEST.in 14B
PKG-INFO 928B
README.txt 21KB
CONTRIBUTORS.txt 120B
CHANGES.txt 16KB
COPYRIGHT.txt 531B
meld3
__init__.py 43KB
_compat.py 4KB
test_meld3.py 61KB
meld3.py 134B
setup.cfg 88B
TODO.txt 1KB
setup.py 1KB
meld3.egg-info
PKG-INFO 928B
SOURCES.txt 291B
top_level.txt 6B
dependency_links.txt 1B
LICENSE.txt 2KB
共 18 条
- 1
资源评论
97205
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功