http://blog.sina.com.cn/s/blog_ad0672d60101l2ml.html

1.express中使用ejs
var express = require('express');//需要安装 express
var app = express();

app.set("view engine","ejs");//模版引擎设置为 ejs

2.文件组织

在express中使用ejs,文件组织遵循express。
.views-------放置动态模版
.public------放置静态网页
.layouts-----放置布局文件

3.基本语法
.<% code %>

  
无缓冲的条件语句元素

.<%= code %>
   
转义HTML,该code并且会打印出来

.<%- code %>
   
非转义的buffering,该code并且会打印出来
    
.<% include file %>
    内嵌别的文件
    
.<% layout(file) -%>
    指定布局文件

.<% script(file) -%>
   
包含js脚本文件
    
.<% stylesheet(file) -%>
   
包含css文件
    
.<% block(code, code) -%>
   
指定块内容    
    
4.基本对象

.scripts
    包含的脚本

.stylesheets
    包含的css

.blocks
    包含的块
    
5.例子

//index.ejs
<% layout('boilerplate') -%>
<% script('foo.js') -%>
<% stylesheet('foo.css') -%>
<h1>I am the
<%=what%>
template</h1>
<% block('header',
"<p>I'm in the
header.</p>")
-%>
<% block('footer',
"<p>I'm in the
footer.</p>")
-%>

//boilerplate.ejs
<!DOCTYPE html>
<html>
  <head>
   
<title>It's
<%=who%></title>

<%-scripts%>
   
<%-stylesheets%>
  </head>
  <body>
   
<header>
     
<%-blocks.header%>
   
</header>
   
<section>
     
<%-body -%>
   
</section>
   
<footer>
     
<%-blocks.footer%>
   
</footer>
  </body>
</html>

//app.js
var express = require('express')
  , engine = require('ejs-locals')
  , app = express();

// use ejs-locals for all ejs templates:
app.engine('ejs', engine);

app.set('views',__dirname + '/views');
app.set('view engine', 'ejs'); // so you can render('index')

// render 'index' into 'boilerplate':
app.get('/',function(req,res,next){
  res.render('index', { what: 'best', who: 'me'
});
});

app.listen(3000);

结果
<!DOCTYPE html>
<html>
  <head>
   
<title>It's
me</title>
   
<script
src="foo.js"></script>

<link rel="stylesheet" href="foo.css"
/>
  </head>
  <body>
   
<header>
     
<p>I'm in the
header.</p>
   
</header>
   
<section>
     
<h1>I am the best
template</h1>
   
</section>
   
<footer>
     
<p>I'm in the
footer.</p>
   
</footer>
  </body>
</html>

express - ejs使用介绍的更多相关文章

  1. nodejs+express+ejs+mongoose实例

    nodejs+express+ejs+mongoose实例 nodejs学得异常痛苦,在这里将学的东西做一番整理,算是自我安慰吧.根据网上todo示例,用express和mongoose重写了部分代码 ...

  2. nodejs创建express+ejs项目

    很多同学跟着 node.js开发指南 学习用 express+ejs 创建项目的时候,却总是无法成功,如: 运行 以下命令去创建 express+ejs 创建项目 $ express -t ejs m ...

  3. 用nodejs,express,ejs,mongo,extjs实现了简单了网站后台管理系统

    源代码下载地址:http://download.csdn.net/detail/guoyongrong/6498611 这个系统其实是出于学习nodejs的目的而改写的系统. 原来的系统前端使用了ex ...

  4. node+express+ejs搭建一个简单的"页面"

    1.建立工程文件夹my_ejs. 2.首先利用npm install express和npm install ejs 下载这两个家伙.至于要不要设置成全局的,看习惯,我习惯性的下载到本项目中的文件夹中 ...

  5. nodejs下express+ejs环境搭建

    nodejs下express+ejs环境搭建   分类: Nodejs 1.进入需要创建项目的目录    cd F:\nodeCode     2.创建一个带ejs模板工程,工程名为haha    e ...

  6. 前端笔记之NodeJS(三)Express&ejs模板引擎&请求识别

    一.Express框架 1.1基本使用 创建http服务器特别麻烦,express框架解决了这个的问题. Express在node界的地位,就相当于jQuery在DOM界的地位.jQuery的核心就是 ...

  7. node入门 express ejs

    hello.js var express = require("express"); var app = express(); app.get("/hello" ...

  8. node.js + express(ejs) + mongodb(mongoose) 增删改实例

    MongoDB 安装步骤总结: 1.解压目录到d盘 mongodb 2.安装目录的下新建文件mongo.config文件 ##store data here dbpath=D:\mongodb\dat ...

  9. Node - EJS模板应用(node+express+ejs)

    准备工作: 工具:Webstorm 1. 新建一个文件夹为blogs(随意). 一个js文件app.js. 一个文件夹views,文件夹内一个index.ejs文件,文件夹asstes内style.c ...

随机推荐

  1. checkbox选中与取消选择

    先上代码 <form> 你爱好的运动是?<br/> <input type="checkbox" name="items" val ...

  2. Asp.net useful tools

    fuslogvw trace the assembly binding when app start up. ILdasm to inspect the manifest of the assembl ...

  3. UE4 编译后 不能正常使用Open Level 打开关卡解决方案:Open Level Blueprint Node not workin

    配置DefaultEditor.ini 文件 [AllMaps] +Map=/关卡文件路径   参考文献: https://answers.unrealengine.com/questions/141 ...

  4. 期望DP

    BZOJ 1415 #include <iostream> #include <cstring> #include <algorithm> #include < ...

  5. 修改FastColoredTextBox控件完成选择

    //判断是否是中文        public bool IsChina(char c)        { bool BoolValue = false;            if (Convert ...

  6. Linux 2>&1理解(转)

    2>&1使用一 相关知识1)默认地,标准的输入为键盘,但是也可以来自文件或管道(pipe |).2)默认地,标准的输出为终端(terminal),但是也可以重定向到文件,管道或后引号(b ...

  7. 修改MySQL用户的密码

    =====知道当前用户密码时===== P.S.:此文只针对windows下的用户密码更改. 1.使用进入MySQL的bin文件夹下: cd path\to\bin\mysqladmin.exe 2. ...

  8. CF 628B New Skateboard --- 水题

    CD 628B 题目大意:给定一个数字(<=3*10^5),判断其能被4整除的连续子串有多少个 解题思路:注意一个整除4的性质: 若bc能被4整除,则a1a2a3a4...anbc也一定能被4整 ...

  9. 在Bootstrap中 强调相关的类

    .text-muted:提示,使用浅灰色(#999) .text-primary:主要,使用蓝色(#428bca) .text-success:成功,使用浅绿色(#3c763d) .text-info ...

  10. vb6 获取 http only 的 Cookie

    Option Explicit ' No more data is available. ' The data area passed to a system call is too small. P ...