简体   繁体   English

纯 JavaScript 的 Jade 模板

[英]Jade template with pure JavaScript

I am using jade template engine in my app and want to remove jquery to check performance.我在我的应用程序中使用 jade 模板引擎,并希望删除 jquery 以检查性能。 Cannot figure out how to do it with pure JS in this example.在这个例子中无法弄清楚如何用纯 JS 来做到这一点。

Instead of using jquery as such:而不是像这样使用jquery:

var items = ['it1', 'it2', 'it3'];
var tmpl = require('views/list');
var html = tmpl({ items: App.items });
$('body').append(html);

Template would look as:模板将如下所示:

ul#itemsLists
    each item in items
    li= item

See this plunker, it is using pure javascript to display your items: http://plnkr.co/edit/g7LQdO6enO2nlKpUDnum?p=preview看到这个 plunker,它使用纯 javascript 来显示您的项目: http ://plnkr.co/edit/g7LQdO6enO2nlKpUDnum?p=preview

script(type='text/javascript').
  var items = ['it1', 'it2', 'it3'];
  var ul = document.getElementById('itemsLists');
  items.forEach(displayElem);
  function displayElem(element, index) {
    var li = document.createElement('li');
    ul.appendChild(li);
    t = document.createTextNode(element);
    li.innerHTML=li.innerHTML + element;
  }

script to use after the body在正文之后使用的脚本

After you can find some template engine js such as pure.js or blueimp to integrate the js into the html but with jade it's not always working (more pure html or ejs).在您可以找到一些模板引擎 js 之后,例如 pure.js 或 blueimp 将 js 集成到 html 中,但是使用 jade 它并不总是有效(更纯的 html 或 ejs)。

Instead of insertBefore, or appendChild, use insertAdjacentHTML.使用 insertAdjacentHTML 代替 insertBefore 或 appendChild。

var tmpl = require('views/list');
var html = tmpl({ items: App.items });

var a = document.querySelector('p');
a.insertAdjacentHTML('afterend', html);

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

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