简体   繁体   English

基于DOM树的JavaScript模板引擎

[英]DOM tree based JavaScript template engines

I am looking for a new Javascript template engine to replace old jQuery Template for my client side templating needs. 我正在寻找一个新的Javascript模板引擎来替换旧的jQuery模板,以满足我的客户端模板需求。

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 . 我更喜欢模板引擎处理DOM树而不是文本字符串的方法,然后将cooked字符串的内容转储到innerHTML This is better performance wise and I find DOM manipulation more proper way of constructing more of DOM tree. 这是更好的性能,我发现DOM操作更适合构建更多DOM树的方法。

What options I do have for Javascript template engine which would directly create DOM trees instead of being text based engines? 我有哪些选项可以直接创建DOM树而不是基于文本的引擎的Javascript模板引擎? I like Mustache.js's logicless approach, but it seems to operate on strings only. 我喜欢Mustache.js的无逻辑方法,但似乎只对字符串进行操作。 Native jQuery integration would also be a nice feature. 原生jQuery集成也是一个很好的功能。

soma-template is a new one. soma-template是一个新的。

Pure DOM manipulation, a lot of features, natural syntax, fully extensible with other libraries such as underscore.string, function calls with parameters, helpers, watchers. 纯DOM操作,许多功能,自然语法,可与其他库完全扩展,如underscore.string,带参数的函数调用,帮助程序,观察者。 Capability to update only some nodes if needed, templates inside the DOM itself. 如果需要,只能更新某些节点的能力,DOM本身内部的模板。

http://soundstep.github.com/soma-template/ http://soundstep.github.com/soma-template/

I've recently created DOM templating engine inspired by PURE and Transparency. 我最近创建了由PURE和Transparency启发的DOM模板引擎。

It supports loops, conditions and much more. 它支持循环,条件等等。

Take a look at doc: http://code.meta-platform.com/metajs/components/template/ 看一下doc: http//code.meta-platform.com/metajs/components/template/

Don't be affraid that MetaJS is big library, templating lib can be used standalone. 不要害怕MetaJS是大型库,模板库可以单独使用。

Short example: 简短的例子:

HTML: HTML:

<div id="tpl">
    <ul id="tasks">
        <li>
            <span class="task"></span>
            <span class="due-date"></span>
        </li>
    </ul>
</div>

Define template: 定义模板:

var tpl = Meta.Template(document.getElementById('tpl'), {
    "ul#tasks li": $__repeat("tasks", {
        ".task":        "task",
        ".due-date":    $__date("d. m. Y", "due_date"),
        "@":            $__attrIf("completed", "complete")
    })
});

Render template: 渲染模板:

tpl({
    tasks: [
        {
            task: "Write concept",
            due_date: new Date(2015, 3, 22, 0, 0, 0, 0),
            complete: true
        }, {
            task: "Consult with customer",
            due_date: new Date(2015, 3, 25, 0, 0, 0, 0),
            complete: false
        }
    ]
});

Result: 结果:

<div id="tpl">

    <ul id="tasks">
        <li>
            <span class="task" completed>Write concept</span>
            <span class="due-date">22. 3. 2015</span>
        </li>
        <li>
            <span class="task">Consult with customer</span>
            <span class="due-date">25. 3. 2015</span>
        </li>
    </ul>

</div>

Take a look at Monkberry DOM template engine . 看看Monkberry DOM模板引擎 Monkberry JavaScript DOM模板引擎

It is really small (just 1,5kB gzipped), dependency free library, server compiling, one-way data binding, and it's dramatically fast! 它非常小(只有1,5kB gzip),无依赖库,服务器编译,单向数据绑定,而且速度非常快!

Here example of template and generated code: 这里是模板和生成代码的示例:

<div>
  <h1>{{ title }}</h1>
  <p>
    {{ text }}
  </p>
</div>

Will generate: 会产生:

var div = document.createElement('div');
var h1 = document.createElement('h1');
var p = document.createElement('p');

div.appendChild(h1);
div.appendChild(p);

   ...

view.update = function (data) {
  h1.textContent = data.title;
  p.textContent = data.text;
};

Monkberry supports if , for and custom tags. Monkberry支持iffor和custom标签。 And has a lot of rendering optimizations. 并且有很多渲染优化。 Templates can be rendered on server with webpack , browserify or cli . 模板可以与服务器呈现webpackbrowserifycli

dna.js is a templating engine that clones DOM elements ( https://dnajs.org ). dna.js是一个克隆DOM元素的模板引擎( https://dnajs.org )。

Example template for a book: 书籍的示例模板:

<h1>Featured Books</h1>
<div id=book class=dna-template>
   <div>Title:  <span>{{title}}</span></div>
   <div>Author: <cite>{{author}}</cite></div>
</div>

Call to insert a copy of the template into the DOM: 调用将模板的副本插入DOM:

dna.clone('book', { title: 'The DOM', author: 'Jan' });

Resulting HTML: 产生的HTML:

<h1>Featured Books</h1>
<div class=book>
   <div>Title:  <span>The DOM</span></div>
   <div>Author: <cite>Jan</cite></div>
</div>

Fiddle with a sample "To-Do Application" : 摆弄一个样本“待办事项”
https://jsfiddle.net/uLrc7kmp https://jsfiddle.net/uLrc7kmp

去做

dna.js was created precisely to avoid constructing and passing around string templates (I'm the project maintainer). dna.js的创建正是为了避免构造和传递字符串模板(我是项目维护者)。

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...) 免费的麻省理工学院许可的jQuery DNA模板与超级大国(您可以将更改的数据重新应用到相同的HTML结构,以更新任何数据更改的UI ...)

https://github.com/webdevelopers-eu/jquery-dna-template/ https://github.com/webdevelopers-eu/jquery-dna-template/

What sort of sugar are you looking for? 你在寻找什么样的糖? The raw DOM api always worked fine for me. 原始的DOM api对我来说总是很好。 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. 如果你真的采用了这个想法,即模板引擎在性能方面不好,如果你想有效地构建一个DOM树,不要使用innerHTML。 What I tend to do is just create elements manually using document.createElement. 我倾向于使用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. 我的模板是通过编写辅助函数来创建的,这些函数创建节点集合并通过设置.innerText属性用数据填充它们。 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. 然后我可以缓存对我希望经常引用的节点的引用,这样我就不必遍历DOM树来再次找到这些节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM