EJS的CommonJs规范版本

ejs分为两个版本一个是CommonJs版本,另外一个是AMD规范的版本.

基础:html页面

安装:<script type="text/javascript" src="./node_modules/ejs/ejs.js"></script>

检查:ejs会将自身挂载到window对象上,所以你只要console.log(ejs)控制台有输出就说明安装成功了.

调试:在浏览器中

注意:和AMD规范版本不同,EJS的CommonJs版本要写在html中

查看是否成功引入ejs

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./ejs.min.js"></script>
<script>
window.onload = function() {
console.log(ejs);
}
</script>
</head>
<body> </body>
</html>

渲染单个数据

方法一:ejs.render(str,data,options)

实例一:

// 14.html内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./ejs.min.js"></script>
<script>
window.onload = function() {
var string = ejs.render('<h2><%= title %></h2>',{title:'hello'});
document.getElementsByTagName('body')[].innerHTML = string;
}
</script>
</head>
<body> </body>
</html> // 在浏览器中打开页面输出 hello

实例二:

// 实际应该展示在页面上的Dom
<div id="interpolation"></div> // 模版
<script id="interpolationtmpl" type="text/x-dot-template">
<!-- 新增用户弹窗 -->
<div id="addSourcePopup" class="modal fade q-popup" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true" data-child-node-id="q-popup" style="margin-top: 10px;">
<div class="q-popup-modal modal-dialog" style="width: 900px;background-color: #ffffff;">
<div class="popup-header modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
{{? it.data && it.data.id}}
更新计划
{{??}}
新增计划
{{?}}
</div>
</div>
</div>
</script> // 编译和渲染
jQuery.ajax({
type: "get",
url: "http://",
dataType: "json",
success: function (result) {
if (result && "" == result.code && result.json) {
var str = ejs.render($("#interpolationtmpl").text(),result.json); // 取到模版并使用数据渲染
    $("#interpolation").html(str); // 添加进之前准备好的Dom
}
}
});

方法二:ejs.compile(str,options)(data)

// 实际应该展示在页面上的Dom
<div id="interpolation"></div> // 模版
<script id="interpolationtmpl" type="text/x-dot-template">
<!-- 新增用户弹窗 -->
<div id="addSourcePopup" class="modal fade q-popup" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true" data-child-node-id="q-popup" style="margin-top: 10px;">
<div class="q-popup-modal modal-dialog" style="width: 900px;background-color: #ffffff;">
<div class="popup-header modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
{{? it.data && it.data.id}}
更新计划
{{??}}
新增计划
{{?}}
</div>
</div>
</div>
</script> // 编译和渲染
jQuery.ajax({
type: "get",
url: "http://",
dataType: "json",
success: function (result) {
if (result && "" == result.code && result.json) {
var template = ejs.compile($("#interpolationtmpl").text()); // 取到模版
var rs = template(result.json); // 使用数据渲染
    $("#interpolation").html(rs); // 添加进之前准备好的Dom
}
}
});

其他用法和AMD规范的版本相同

最新文章

  1. Linux实战教学笔记02:计算机系统硬件核心知识
  2. 海外建VPS并支持VPN
  3. java编程思想-java IO系统
  4. WPF的图片操作效果(一):RenderTransform
  5. 支持nmap批量漏洞扫描的script
  6. java面向对象编程——第五章 对象的行为
  7. JSF 2 dropdown box example
  8. python JSON处理
  9. Bellman-Bord(贝尔曼-福特)
  10. Eclipse快捷键 10个最有用的快捷键(转)
  11. [目录][总结] C++和Java 中的主要操作对比
  12. HTML常用标签属性图
  13. SPOJ 375(树链剖分)
  14. ROS机器人程序设计(原书第2版)补充资料 (零) 源代码、资料和印刷错误修订等 2017年01月01日更新
  15. JavaScript中闭包的写法和作用详解
  16. 【原】使用vue2+vue-router+vuex写一个cnode的脚手架
  17. windows下安装ElasticSearch 5
  18. JS面试题(二)(常见算法编程)
  19. char varchar
  20. 2018.06.27The Windy's(费用流)

热门文章

  1. LeetCode 36. 有效的数独(Valid Sudoku)
  2. Ubuntu14.04升级cmake版本的方法
  3. Python学习笔记—Dict和set
  4. DFA和NFA的区别
  5. modprobe 和 insmod 区别
  6. AMBARI部署HADOOP集群(3)
  7. 自定义组合控件SettingItemView的简单实现
  8. openstack部署keystone
  9. 根据json生成java实体类
  10. MySQL乱码的原因和设置UTF8数据格式