哪些模板语言将 HTML *输出为节点树*?

发布于 2024-07-20 14:44:27 字数 690 浏览 2 评论 0原文

首先,HTML 是一棵节点树。 这不仅仅是一段文字。

然而,大多数模板引擎处理它们的输入和输出,因为它只是一个文本。 他们不关心他们的标签、他们的 {$foo}<% bar() %> 周围发生了什么; 他们也不关心他们输出什么。 有时他们碰巧生成了正确的 html,但这只是巧合; 他们的目的不是这个,他们想要的只是用他们的评价替换文本流中的一些有趣的标记。

有一些模板引擎确实将其输出视为一组节点; 我想到了 XSLT 和 Haml。 对于某些任务,这具有优点:例如,您可以自动重新格式化(例如,删除所有空文本节点;自动缩进;自动换行)。 结果保证是正确的 xml/sgml,除非您使用可能破坏该结果的严格操作子集。 此外,这样的模板引擎会自动引用字符串,在文本节点和属性中不同,因为它严格知道您正在编写属性还是文本节点。 此外,它可以有条件地从输出中删除节点,因为它知道节点的开始和结束位置(这很有用),并且可以执行其他重要的节点操作。

您可能不喜欢 XSLT 的冗长或功能,但是您的模板是可 xmllint 的 XML,并且您的输出是良好的 sgml/xml,这真是太有帮助了。

所以问题是:您知道哪些模板引擎将其输出视为一组正确的节点,而不仅仅是非结构化文本? 我知道 XSLT、Haml 和一些基于 Python 的晦涩难懂的语言。 摩尔!

HTML is a tree of nodes, before all. It's not just a text.

However, most templating engines handle their input and output as it was just a text; they don't care what happens around their tags, their {$foo}'s and <% bar() %>'s; also they don't care about what are they outputting. Sometimes they happen to produce a correct html, but that's just a coincidence; they didn't aim for that, all they wanted is to replace some funny marks in the text stream with their evaluation.

There are a few templating engines which do treat their output as a set of nodes; XSLT and Haml come to mind. For some tasks, this has advantages: for example, you can automatically reformat (like, delete all empty text nodes; auto-indent; word-wrap). The result is guaranteed to be a correct xml/sgml unless you use a strict subset of operations that can break that. Also, such templating engine would automatically quote strings, differently in text nodes and in attributes, because it strictly knows whether you're writing an attribute or a text node.
Moreover, it can conditionally remove a node from output because it knows where it does begin and end, which is useful, and do other non-trivial node operations.

You might not like XSLT for its verbosiness or functionalness, but it's damn helps that your template is xmllint-able XML, and your output is a good sgml/xml.

So the question is: Which template engines do you know that treat their output as a set of correct nodes, not just an unstructured text?
I know XSLT, Haml and some obscure python-based one. Moar!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

三五鸿雁 2024-07-27 14:44:27

你知道哪些模板引擎
将它们的输出视为一组
正确的节点

令人惊讶的是 ASP.NET 确实如此! 如果需要,您可以通过某种 DOM 更改页面的 HTML 输出: http: //en.wikipedia.org/wiki/Asp.net#Rendering_technique

Which template engines do you know
that treat their output as a set of
correct nodes

Surprisingly ASP.NET does! You can change the HTML output of the page through a kind of DOM if you want: http://en.wikipedia.org/wiki/Asp.net#Rendering_technique

眼泪也成诗 2024-07-27 14:44:27

尽管这些都是嵌入式 DSL,取决于它们的宿主语言

though these are all embedded DSLs, dependent on their host language

海未深 2024-07-27 14:44:27

TAL(最初是 Zope 的一部分,但现在以多种语言实现)是基于 XML 的。 这是非常合乎逻辑和意图揭示的工作 - 而不是塞进一堆文本,你告诉模板类似“将此链接的 href 属性设置为 http://google.com/ 并将其文本内容设置为“Search Google”。您不必管理哪些字符串需要转义 - 通常,如果您打算这样做的话。被解释为标记,您将其放入模板中,如果您不希望将其解释为标记,则可以将其作为属性值或文本内容输入,TAL 将正确转义它。

TAL (originally part of Zope but now implemented in a variety of languages) is XML-based. It's very logical and intention-revealing to work with - instead of shoving in a heap of text you're telling the template something like "set the href attribute of this link to http://google.com/ and set its text content to 'Search Google'. You don't have to manage which strings need to be escaped - generally if you intend something to be interpreted as markup, you put it in a template, and if you don't intend it to be interpreted as markup you feed it in as an attribute value or text content and TAL will escape it correctly.

赠我空喜 2024-07-27 14:44:27

基本上所有模板引擎都使用 XML 作为其文件格式(用于定义模板)。 通过使用 XML,它们强制文件必须格式正确。

[编辑] 示例为: Genshi (Python) 或 JSP 2.0 (Java)。

Basically all templating engines which use XML as their file format (for defining templates). By using XML, they enforce that the file must be well-formed.

[EDIT] Examples are: Genshi (Python) or JSP 2.0 (Java).

情独悲 2024-07-27 14:44:27

使用 Nagare Web 框架,视图始终是直接构建的 XML 节点树在Python中。
然后可以在 Python 中操作该树,使用 XSL 进行转换,在 HTML 或 XHTML 中序列化...

(“nagare.namespaces”包随 Nagare projet 一起提供,但可以在任何 Python 应用程序中使用)

示例:

>>> from nagare.namespaces import xhtml_base
>>> h = xhtml_base.Renderer()    # The XHTML tree builder
>>>
>>> # Building the tree of nodes
>>> with h.html:
>>>    with h.body:
>>>        h << h.h1('Hello world')
>>> tree = h.root  # The tree root element
>>>
>>> print tree.write_xmlstring() # Tree serialized in XML

<html><body><h1>Hello world</h1></body></html>

With the Nagare web framework, the views are always a tree of XML nodes, directly built in Python.
The tree can then be manipulated in Python, transformed with XSL, serialized in HTML or XHTML ...

(the 'nagare.namespaces' package comes with the Nagare projet but can be used in any Python application)

Example:

>>> from nagare.namespaces import xhtml_base
>>> h = xhtml_base.Renderer()    # The XHTML tree builder
>>>
>>> # Building the tree of nodes
>>> with h.html:
>>>    with h.body:
>>>        h << h.h1('Hello world')
>>> tree = h.root  # The tree root element
>>>
>>> print tree.write_xmlstring() # Tree serialized in XML

<html><body><h1>Hello world</h1></body></html>
无尽的现实 2024-07-27 14:44:27

我在这里维护了推送式模板系统的列表:
http://perlmonks.org/?node_id=674273

我正在评估各种 Perl其分离指数的模板系统:
http://bit.ly/bXaYt7

但是基于树的是我写的,HTML::Seamstress -
http://search.cpan.org/dist/HTML-Seamstress/

术语“推式”来自 Terence Parr 的论文“Enforcing Strict Model-View
模板引擎中的分离”-
http://www.cs.usfca.edu/~parrt/论文/mvc.templates.pdf

I maintain a list of push-style templating systems here:
http://perlmonks.org/?node_id=674273

And am in the process of evaluating various Perl templating systems for their separation index:
http://bit.ly/bXaYt7

But the tree-based one is written by me, HTML::Seamstress -
http://search.cpan.org/dist/HTML-Seamstress/

The term "push-style" comes from Terence Parr's Paper "Enforcing Strict Model-View
Separation in Template Engines" -
http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf

萌面超妹 2024-07-27 14:44:27

在Scheme 中有一种表示 XML(或其子集,例如 XHTML)的标准方法,称为 SXML 。 在我看来,这是使方案成为一个好的事情之一Web 开发语言。 可以将文档的内容构建为本机方案列表,然后在一个函数调用中将其呈现为(正确的)XHTML。

下面是一个示例,它采用一个简单的文本字符串,并将其包装为单段落 HTML 页面的内容。 所以函数 as-page 充当模板; 它的输出是一个方案列表,可以很容易地转换为等效的 HTML。 这种方法不可能出现不平衡或格式错误的标签。

(use-modules (sxml simple))

(define (as-page txt)
  `(html
    (head (title "A web page"))
    (body (p ,txt))))

(as-page "It works!!!!!")
;; $2 = (html (head (title "A web page")) (body (p "It works!!!!")))

(sxml->xml (as-page "It works!!!!"))
;; $3 = <html><head><title>A web page</title></head><body><p>It works!!!!</p></body></html>

There is a standard way of representing XML (or a subset of it such as XHTML) in Scheme known as SXML. This is one of the things that, in my opinion, make Scheme a good language for web development. It is possible to build up the contents of a document as a native Scheme list, and then render this to (correct) XHTML in one function call.

Here is an example that takes a simple text string, and wraps it as the contents of a one-paragraph HTML page. So the function as-page is acting as a template; its output is a Scheme list which can be easily translated to its equivalent HTML. Unbalanced or malformed tags are not possible with this approach.

(use-modules (sxml simple))

(define (as-page txt)
  `(html
    (head (title "A web page"))
    (body (p ,txt))))

(as-page "It works!!!!!")
;; $2 = (html (head (title "A web page")) (body (p "It works!!!!")))

(sxml->xml (as-page "It works!!!!"))
;; $3 = <html><head><title>A web page</title></head><body><p>It works!!!!</p></body></html>
疯狂的代价 2024-07-27 14:44:27

另外,来自 Haskell 快照的 http://snapframework.com/docs/tutorials/heist 似乎合身。

Also, http://snapframework.com/docs/tutorials/heist from Haskell's snap seems to fit.

合约呢 2024-07-27 14:44:27

好未来绝对不是推式的。 它可能是基于 XML 的,但它是拉式(推式的最简陋形式)。

TAL is absolutely not push-style. It may be XML-based but it is pull-style (the most degenerate form of push-style).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文