是否有优秀的 Python 库可以为所谓的“模板动画”、“不引人注目的服务器脚本”或“将数据绑定到标记”提供优雅的解决方案?
换句话说,我正在寻找一个好的 API 将数据绑定到纯 HTML 以生成动态网页。
如果它将 API 调用和 HTML 编译为 HTML 发出代码,则会有奖励点,因为它解析 HTML、操作它然后序列化回 HTML 的速度很慢。
背景
这个问题是由“你的模板引擎很糟糕,你写的所有东西都是意大利面条代码(是的) ,你)”的博客文章提倡纯 HTML 的模板动画,以避免语言固有的混合到模板系统。
不要将数据绑定和标记混合在单个模板 (view.tmpl) 中,而是将数据绑定分离到其自己的代码文件 (view.py),只在模板 (view.html) 中保留 HTML。
拆分的明显好处是设计人员可以独立于开发人员和后端实现以 HTML 形式编写视图。然后,设计人员可以在他选择的工具集中迭代地改进视图 HTML。设计人员通常不乐意使用模板语言并运行完整的开发环境和 Web 服务器来可视化更改。
HTML 动画
性能缓慢的潜在缺点。解析 HTML,操作结构来绑定数据,然后序列化回 HTML,这比处理已编译模板慢 3 倍。毕竟,模板语言实际上是打印 HTML 代码的语法糖。 hquery Ruby 库通过将 hquery 代码和 HTML 编译成传统模板来解决这个问题。很好地展示了基于 Ruby 的 DSL 的强大功能。可以用 Python 完成吗?
-
仅限于 HTML 和 XML。模板动画依赖于 HTML 的结构,相比之下,许多模板语言并不关心标记,并且会像生成 HTML 一样愉快地生成纯文本或 CSS。我不认为这对于 Web 应用程序来说是一个问题,一些模板语言(例如 Genshi)以在有效 XML 结构内工作而自豪。
-
太复杂。使用 DOM 操作 HTML 是非常痛苦的。希望正确的 API 能够使在代码中操作 HTML 就像向 HTML 添加模板指令一样简单。
研究
Ruby的非 Python 解决方案
hquery,被描述为“不引人注目的服务器脚本”,是一种 DSL从数据结构填充 HTML。这些 HTML 操作编译为 ERB 模板以便快速执行。
-
queryTemplates for PHP,被描述为“DOM 和 CSS 驱动模板”,提供“数据注入”方法”。 API 使用受 jQuery 启发的调用链。该代码描述了如何将 PHP 数据结构绑定到 HTML,并将结果编译为 .php 模板以接受具体的数据结构
纯 JavaScript 库,用于将 JSON 绑定到 HTML。需要额外的“指令”JSON 来描述如何将 JSON 数据与 HTML 元素关联起来。
-
diazo 用于 Python。需要一个rules.xml,它与设计者的纯HTML主题一起编译为XSLT“模板”,该模板将动态HTML或XML从web应用程序绑定到主题。
queryTemplates、pure 和 diazo 需要如何将数据绑定到 HTML 的抽象描述 - 分别以链式 API 调用、JSON 和 XML 规则的形式。然后系统编译一个模板以接受具体数据进行绑定。抽象数据绑定描述添加了一个间接层,我怀疑这比向 HTML 添加模板指令需要更多的心理体操。
然而,hquery 的独特之处在于它的 API 调用接受数据查找表达式,这些表达式在编译的 ERB 中以某种方式保持未计算并保留。这是一种类似于 Lisp 的“代码即数据”操作模式,至少非常酷,而且可能更容易编写。
其他有趣的 Python 库
PyMeld,一个基于字符串替换的类似 DOM 的库。我尝试了一下,发现很麻烦。填充表涉及从表中剪切示例行,然后用记录数据重复填充该行并将副本附加到表中。呃。
-
plates for Node.js“将数据绑定到标记”(即 JSON 到 HTML)。它就像没有任何 Mustache 模板语法的 Mustache 。我预计从 JSON 直接填充 HTML 会限制它非常简单的数据绑定。甚至没有看到循环示例。
-
HTML::Zoom 为珍珠。它是 Perl 的 jQuery。不编译。
Python
lxml.etree 的起点
- ,事实上的 Python XML/HTML 库。也许痛苦只有 DOM 的一半。编写用于解析/生成 XML,而不是将数据绑定到现有 HTML。但它是构建模板动画库的良好基础。
-
pyquery 构建在 lxml.etree 和 lxml.cssquery 之上。逐个模拟 jQuery API。这使得它更适合 HTML 操作,但缺乏将数据结构绑定到 HTML 的便捷功能。这些可以添加,唯一的缺点是缓慢,因为它本质上是一个解析-操作-序列化过程。
-
golem 由brandizzi 开发,构建于lxml.etree 和lxml.cssquery 之上。它才刚刚开始,而是一个专门用于数据绑定的程序 API。
即使有一个很好的 API 将数据绑定到 HTML,它也会很慢,除非它可以编译成 HTML 生成代码,或者至少编译成传统的模板语言。
处理包含和母版
处理共享页脚或导航的包含以及处理布局母版(也称为模板继承)是一个相关的问题。纯 HTML 需要完整并扩展才能用作预览,因此这些扩展发生在数据绑定步骤之前。存在管理常见包含和布局的工具 - DreamWeaver 有一个用于共享页脚等的库系统。
Are there good Python libraries that provide an elegant solution for what is called "template animation", "unobtrusive server scripting" or "binding data to markup"?
In other words, I'm looking for a good API to bind data to plain HTML to produce a dynamic web page.
Bonus points if it compiles the API calls and HTML into HTML-emitting code, since its slow to parse HTML, manipulate it and then serialise back to HTML.
Background
This question is prompted by the "Your templating engine sucks and everything you have ever written is spaghetti code (yes, you)" blog post which advocates template animation of plain HTML to avoid the mixing of languages inherent to templating systems.
Instead of mixing data-binding and markup in a single template (view.tmpl), separate the data-binding to its own code file (view.py), leaving only HTML in the template (view.html).
The clear benefit of the split is designers can write views in HTML independently of the developers and back-end implementation. The designer can then iteratively improve the view HTML in his chosen toolset. Designers aren't generally happy to hack away in a template language and run a full dev environment and web server to visualise the changes.
Potential drawbacks of animating HTML
-
Slow performance. Parse HTML, manipulate the structure to bind data, then serialise back to HTML, easily 3x slower than processing a compiled template. Template languages after all are really syntactic sugar for code that prints HTML. The hquery Ruby library solves this by compiling the hquery code and HTML into traditional template. Great demonstration of the power of Ruby-based DSLs. Can it be done in Python?
-
Limited to HTML and XML. Template animation relies on the structure of HTML, in constrast to many templating languages don't care about the markup is and will just as happily generate plain text or CSS as they will HTML. I don't think this is an issue for web apps, and some templating languages like Genshi pride themselves on working within the structure of valid XML.
-
Too complex. Manipulating HTML with DOM is downright painful. Hopefully the right API will make manipulating HTML in code just as easy as adding template instructions to the HTML.
Investigation
non-Python solutions
-
hquery for Ruby, described as "unobtrusive server scripting", is a DSL that fills HTML from data structures. These HTML manipulations compile to an ERB template for fast execution.
-
queryTemplates for PHP, described as "DOM and CSS driven templating" providing "data injection methods". API uses jQuery-inspired call chaining. The code describes how to bind a PHP data structure to the HTML, and compiles the result to a .php template to accept the concrete data structure
-
pure JavaScript library for binding JSON to HTML. Requires additional "directive" JSON describing how to relate the JSON data to HTML elements.
-
diazo for Python. Requires a rules.xml which together with the designer's plain HTML theme is compiled to an XSLT "template" that binds dynamic HTML or XML from the webapp to the theme.
queryTemplates, pure and diazo require an abstract description of how to bind data to HTML - in the form of chained API calls, JSON, and XML rules respectively. The system then compiles a template to accept concrete data for binding. The abstract data-binding description adds a layer of indirection which I suspect require more mental gymnastics to use than adding template directives to HTML.
hquery however sets itself apart by having its API calls accept data lookup expressions that are somehow left unevaluated and retained in the compiled ERB. It's a lisp-like "code as data" mode of operation that is at least very cool and is probably easier to write.
Other interesting libraries
-
PyMeld for Python, a DOM-alike based on string replacement. I tried it and found it cumbersome. Filling a table involves cutting the sample row from the table, then repeatedly populating the row with record data and appending a copy to the table. Ugh.
-
plates for Node.js "binds data to markup" (namely, JSON to HTML). It's like Mustache without any Mustache template syntax. I expect the direct filling of HTML from JSON limits it very simple data binding. Didn't even see a looping example.
-
HTML::Zoom for Pearl. It's jQuery for Perl. Doesn't compile down.
Starting points for Python
-
lxml.etree the de-facto Python XML/HTML library. Maybe half as painful as DOM. Was written for parsing/generation of XML, not data binding to existing HTML. But it's a decent foundation on which to build a template animation library.
-
pyquery built on top of lxml.etree and lxml.cssquery. Emulates the jQuery API method-by-method. This makes it superior for HTML manipulation, but lacks convenience functions for binding data structures to the HTML. These can be added, only draw back would be slowness because its inherently a parse-manipulate-serialise process.
-
golem by brandizzi, built on top of lxml.etree and lxml.cssquery. It's just starting out, but a procedural API specifically for data binding.
Even with a good API for binding data to HTML, it will be slow unless it can be compiled into HTML-emitting code, or at least into a traditional templating language.
Handling includes and masters
Handling inclusion of shared footers or navigation, and handling layout masters (aka template inheritance) is a related problem. The plain HTML needs to be complete and expanded to be usable as a preview, so those expansions happen before the data binding step. Tools exist to manage common includes and layouts - DreamWeaver for one has a library system for shared footers and the like.
发布评论