我正在寻找一个新的 Javascript 模板引擎来替换旧的 jQuery 模板,以满足我的客户端模板需求。
我更喜欢模板引擎处理 DOM 树而不是文本字符串的方法,然后将经过处理的字符串的内容转储到 innerHTML
中。这是更好的性能,我发现 DOM 操作是构建更多 DOM 树的更合适的方法。
对于 Javascript 模板引擎,我有哪些选项可以直接创建 DOM 树而不是基于文本的引擎?我喜欢 Mustache.js 的无逻辑方法,但它似乎只对字符串进行操作。原生 jQuery 集成也是一个不错的功能。
I am looking for a new Javascript template engine to replace old jQuery Template for my client side templating needs.
I'd prefer approach where the template engine deals with DOM trees instead of text strings and later dumps the content of the cooked string into innerHTML
. This is better performance wise and I find DOM manipulation more proper way of constructing more of DOM tree.
What options I do have for Javascript template engine which would directly create DOM trees instead of being text based engines? I like Mustache.js's logicless approach, but it seems to operate on strings only. Native jQuery integration would also be a nice feature.
发布评论
评论(9)
透明度:
https://github.com/leonidas/transparency/
纯粹:
http://beebole.com/pure/documentation/
板
https://github.com /flatiron/plates
为什么全部这个:
http://blog.nodejitsu.com/micro-templates-are-dead
Transparency:
https://github.com/leonidas/transparency/
PURE:
http://beebole.com/pure/documentation/
Plates
https://github.com/flatiron/plates
Why all this:
http://blog.nodejitsu.com/micro-templates-are-dead
远端
http://code.google.com/p/distal
Distal
http://code.google.com/p/distal
soma-template 是一个新模板。
纯 DOM 操作,大量功能,自然语法,可与其他库完全扩展,例如 underscore.string、带参数的函数调用、帮助程序、观察程序。如果需要,能够仅更新某些节点、DOM 本身内的模板。
http://soundstep.github.com/soma-template/
soma-template is a new one.
Pure DOM manipulation, a lot of features, natural syntax, fully extensible with other libraries such as underscore.string, function calls with parameters, helpers, watchers. Capability to update only some nodes if needed, templates inside the DOM itself.
http://soundstep.github.com/soma-template/
这是关于 7 个著名 JS 模板引擎的很好的比较: http://blog.stevensanderson.com/2012/08/01/rich-javascript-applications-the-seven-frameworks-throne-of-js-2012/
This is good comparison about 7 famous JS template engine: http://blog.stevensanderson.com/2012/08/01/rich-javascript-applications-the-seven-frameworks-throne-of-js-2012/
我最近受 PURE 和 Transparency 的启发创建了 DOM 模板引擎。
它支持循环、条件等等。
看一下文档: http://code.meta-platform.com/metajs/ Components/template/
不要担心 MetaJS 是个大库,模板库可以独立使用。
简短示例:
HTML:
定义模板:
渲染模板:
结果:
I've recently created DOM templating engine inspired by PURE and Transparency.
It supports loops, conditions and much more.
Take a look at doc: http://code.meta-platform.com/metajs/components/template/
Don't be affraid that MetaJS is big library, templating lib can be used standalone.
Short example:
HTML:
Define template:
Render template:
Result:
查看 Monkberry DOM 模板引擎。
它非常小(压缩后只有 1.5kB)、无依赖库、服务器编译、单向数据绑定,而且速度非常快!
以下是模板和生成代码的示例:
将生成:
Monkberry 支持
if
、for
和自定义标签。并且有很多渲染优化。模板可以使用
webpack
、browserify
或cli
在服务器上呈现。Take a look at Monkberry DOM template engine.
It is really small (just 1,5kB gzipped), dependency free library, server compiling, one-way data binding, and it's dramatically fast!
Here example of template and generated code:
Will generate:
Monkberry supports
if
,for
and custom tags. And has a lot of rendering optimizations.Templates can be rendered on server with
webpack
,browserify
orcli
.dna.js 是一个克隆 DOM 元素的模板引擎 (https://dnajs.org )。
书籍的示例模板:
调用将模板的副本插入到 DOM 中:
生成的 HTML:
摆弄示例“待办事项应用程序”:
https://jsfiddle.net/uLrc7kmp
dna.js 的创建正是为了避免构建和传递字符串模板(我是 项目维护者)。
dna.js is a templating engine that clones DOM elements (https://dnajs.org).
Example template for a book:
Call to insert a copy of the template into the DOM:
Resulting HTML:
Fiddle with a sample "To-Do Application":
https://jsfiddle.net/uLrc7kmp
dna.js was created precisely to avoid constructing and passing around string templates (I'm the project maintainer).
具有超能力的免费 MIT 许可 jQuery DNA 模板(您可以重新申请将更改后的数据更改为相同的 HTML 结构,以在任何数据更改时更新 UI...)
https://github.com/webdevelopers-eu/jquery-dna-template/
Free MIT-licensed jQuery DNA Template with superpowers (you can re-apply the changed data to the same HTML structure to update UI on any data change...)
https://github.com/webdevelopers-eu/jquery-dna-template/
您在寻找哪种糖?原始 DOM api 对我来说总是工作得很好。如果您确实接受模板引擎在性能方面不好的想法,那么如果您想有效地构建 DOM 树,请不要使用innerHTML。我倾向于使用 document.createElement 手动创建元素。我的模板是通过编写辅助函数创建的,这些函数创建节点集合并通过设置 .innerText 属性用数据填充它们。然后,我可以缓存对我希望经常引用的节点的引用,这样我就不必再次遍历 DOM 树来查找这些节点。
What sort of sugar are you looking for? The raw DOM api always worked fine for me. If you are really adopting this idea that the templating engines are no good in terms of performance, don't use innerHTML if you want to efficiently build up a DOM tree. What I tend to do is just create elements manually using document.createElement. My templates are created by writing helper functions that create collection of nodes and populate them with the data by setting the .innerText property. I can then cache the references to nodes which I wish to refer to frequently so that I don't have to traverse the DOM tree to find these nodes again.