JavaScript 支持哪些好的模板语言? [英] What good template language is supported in JavaScript?

查看:30
本文介绍了JavaScript 支持哪些好的模板语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板在成熟的编程语言中是一项非常健康的业务,但有没有可以用 JavaScript 处理的好的模板?

Templates are a pretty healthy business in established programming languages, but are there any good ones that can be processed in JavaScript?

模板"是指接受数据对象作为输入、将数据插入某种序列化标记语言并输出标记的文档.众所周知的例子是 JSP,原始的 PHP,XSLT.

By "template" I mean a document that accepts a data object as input, inserts the data into some kind of serialized markup language, and outputs the markup. Well-known examples are JSP, the original PHP, XSLT.

我所说的好"是指 HTML 作者编写声明式且易于编写,它很健壮,并且其他语言也支持它.比我所知道的选项更好的东西.不好"的一些例子:

By "good" I mean that it's declarative and easy for an HTML author to write, that it's robust, and that it's supported in other languages too. Something better than the options I know about. Some examples of "not good":

字符串数学:

element.innerHTML = "<p>Name: " + data.name
    + "</p><p>Email: " + data.email + "</p>";

显然太笨重,HTML 结构不明显.

clearly too unwieldy, HTML structure not apparent.

XSLT:

<p><xsl:text>Name: </xsl:text><xsl:value-of select="//data/name"></p>
<p><xsl:text>Email: </xsl:text><xsl:value-of select="//data/email"></p>

//从结构上来说这很有效,但让我们面对现实吧,XSLT 使 HTML 开发人员感到困惑.

// Structurally this works well, but let's face it, XSLT confuses HTML developers.

修剪路径:

<p>Name: ${data.name}</p><p>Email: ${data.email}</p>

//这很好,但处理器只支持 JavaScript,而且语言有点原始 (http://code.google.com/p/trimpath/wiki/JavaScriptTemplateSyntax).

// This is nice, but the processor is only supported in JavaScript, and the language is sort of primitive (http://code.google.com/p/trimpath/wiki/JavaScriptTemplateSyntax).

我很想看到 JSP、ASP 或 PHP 的一个子集移植到浏览器,但我还没有找到.

I'd love to see a subset of JSP or ASP or PHP ported to the browser, but I haven't found that.

现在人们在 JavaScript 中使用什么来制作模板?

What are people using these days in JavaScript for their templating?

几个月后,这里发布了大量可行的模板语言,但其中大部分都无法在任何其他语言中使用.大多数模板不能在 JavaScript 引擎之外使用.

After a few months there have been plenty of workable template languages posted here, but most of them aren't usable in any other language. Most of these templates couldn't be used outside a JavaScript engine.

微软的例外——您可以在浏览器或任何其他 ASP 引擎中处理相同的 ASP.这有其自身的一组可移植性问题,因为您必须使用 Microsoft 系统.我将其标记为答案,但仍然对更便携的解决方案感兴趣.

The exception is Microsoft's -- you can process the same ASP either in the browser or in any other ASP engine. That has its own set of portability problems, since you're bound to Microsoft systems. I marked that as the answer, but am still interested in more portable solutions.

摆脱这个老问题,十年后,Mustache 得到了数十种语言的广泛支持.现在是当前答案,以防有人仍在阅读.

Dusting off this old question, it's ten years later, and Mustache is widely supported in dozens of languages. It is now the current answer, in case anyone is still reading this.

推荐答案

您可能想查看 Mustache- 它是真正可移植且简单的模板语言,在其他语言中支持 javascript.

You might want to check out Mustache - it's really portable and simple template language with javascript support among other languages.

这篇关于JavaScript 支持哪些好的模板语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆