nodejs express生成的jade模板引擎基本语法举例

doctype html
html
head
title= “nodejs express生成的jade模板引擎基本语法举例”
meta(name=”description” content=”nodejs express生成的jade模板引擎基本语法举例”)
script(src=”scripts/jquery.js” type=”text/javascript”)
link(rel=’stylesheet’, href=’/stylesheets/style.css’)
script alert(“5555″)

body
h1 nodejs express生成的jade模板引擎基本语法举例
p hello world
div(class=”fff” id=”kk”) hello nodejs
script alert(“8888″)

生成对应的html代码为

<!DOCTYPE html>
<html>
<head>
<title>nodejs express生成的jade模板引擎基本语法举例</title>
<meta name=”description” content=”nodejs express生成的jade模板引擎基本语法举例”>
<script src=”scripts/jquery.js” type=”text/javascript”></script>
<link rel=”stylesheet” href=”/stylesheets/style.css”>
<script>alert(“5555″)</script>
</head>
<body>
<h1>nodejs express生成的jade模板引擎基本语法举例</h1>
<p>hello world</p>
<div id=”kk” class=”fff”>hello nodejs</div>
<script>alert(“8888″)</script>
</body>
</html>

 

当然也可以直接在jade里写html代码,变量的引用方法依然是#{name},其中name是变量名,在对应的js文件中定义。如hello.js:

var express = require(‘express’);
var router = express.Router();

/* GET hello listing. */
router.get(‘/’, function(req, res, next) {
res.render(‘hello’, { title: ‘hello world’,name:”8888888” });
});

module.exports = router;

当然如果不习惯ejs模板,可以对app.js做如下修改:

  1. 增加代码“var ejs = require(‘ejs’);”
  2. 将“app.set(‘view engine’, ‘ejs’);” 改成 “app.engine(‘.html’, ejs.__express); app.set(‘view engine’, ‘html’);”

未经允许不得转载:前端撸码笔记 » nodejs express生成的jade模板引擎基本语法举例

上一篇:

下一篇: