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规范的版本相同

EJS学习(五)之EJS的CommonJs规范版本的更多相关文章

  1. EJS学习(一)之特性、安装、工作原理

    前言 EJS,"E" 代表 "effective",即[高效],EJS 是一套简单的JavaScript模板,EJS 没有如何组织内容的教条:也没有再造一套迭代 ...

  2. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  3. 该如何理解AMD ,CMD,CommonJS规范--javascript模块化加载学习总结

    是一篇关于javascript模块化AMD,CMD,CommonJS的学习总结,作为记录也给同样对三种方式有疑问的童鞋们,有不对或者偏差之处,望各位大神指出,不胜感激. 本篇默认读者大概知道requi ...

  4. NodeJS学习笔记—1.CommonJS规范

    由于现在web开发,越来越重视代码的复用和抽象的封装,为了解决代码的组织结构.管理.复用和部署等问题,现在普遍采用的机制是模块机制(module).CommonJS约定桌面应用程序和服务器应用程序需要 ...

  5. EJS学习(三)之语法规则中

    ⚠️实例均结合node,也就是AMD规范版本 ejs中使用render()表示渲染文本 接收三个参数:模版字符串.data.options,返回一个字符串 const ejs = require('e ...

  6. 学习笔记:CommonJS规范、AMD规范

    CommonJS规范 http://wiki.jikexueyuan.com/project/webpack-handbook/commonjs.html CommonJS 规范 http://www ...

  7. EJS学习(二)之语法规则上

    标签含义 <% %> :'脚本' 标签,用于流程控制,无输出即直接使用JavaScript语言. <%= %>:转义输出数据到模板(输出是转义 HTML 标签)即在后端定义的变 ...

  8. NodeJS新建服务器以及CommonJS规范

    1.什么是node.js?(1)Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效.( ...

  9. Commonjs规范及Node模块实现

    前面的话 Node在实现中并非完全按照CommonJS规范实现,而是对模块规范进行了一定的取舍,同时也增加了少许自身需要的特性.本文将详细介绍NodeJS的模块实现 引入 nodejs是区别于java ...

随机推荐

  1. Activity的screenOrientation属性

    activity在屏幕当中显示的方向.属性值可以是下表中列出的一个值: "unspecified" 默认值,由系统来选择方向.它的使用策略,以及由于选择时特定的上下文环境,可能会因 ...

  2. Docker安装ElasticSearch 版本7.1.1

    一.Docker 部署 ElasticSearch 1.从仓库中查找所有ElasticSearch的镜像 [root@iZwz99dhxbd6xwly17tb3bZ app]# docker sear ...

  3. 如何查看MySQL connection id连接id

    每个MySQL连接,都有一个连接ID,可以通过 connection_id()查看. 连接id也可以通过以下方式查看: show processlist中id列 information_schema. ...

  4. Linux 通道

    简单地说,一个通道接受一个工具软件的输出,然后把那个输出输入到其它工具软件.使用UNIX/Linux的词汇,这个通道接受了一个过程的标准输出,并把这个标准的输出作为另一个过程的标准输入.如果你没有重新 ...

  5. 关于Android Studio加载.so文件问题

    在main文件下创建jniLibs文件,然后把.so文件copy过去,然后在 app的 build.gradle里面添加如下代码 sourceSets { main { jniLibs.srcDirs ...

  6. 自定义有焦点的TextView实现广告信息左右一直滚动的跑马灯效果

    import android.content.Context; import android.text.TextUtils; import android.util.AttributeSet; imp ...

  7. 使用 certbot 自动给 nginx 加上 https

    概述 目前,Let's Encrypt 可以算是最好用的 https 证书申请网站了吧.而 certbot 可以算是它的客户端,能够很方便的自动生成 https 证书.我把自己的使用经历记录下来,供以 ...

  8. U盘文档自动备份

    检测到插入U盘即复制其中doc.ppt文件到指定目录 (ucopy.bat): @echo off :again del /Q /f "%temp%\copy.tmp" >n ...

  9. freetye2使用

    使用环境和版本:qt ubuntu 16.04   freetype-2.10.0 1.下载 https://sourceforge.net/projects/freetype/files/freet ...

  10. 基于OpenCV的循环行、列移动函数circShift()

    ///*12 在Matlab中有个circShift()函数,可以实现行.列的循环移动 /// 在返卷积运算中,会用到这个函数.所以,在Opencv中我也定义同样 /// 功能的函数 /// 该函数有 ...