文/玄魂
目錄
1.3 創建測試頁面
使用 vs創建的express站點,默認使用的是jade模板引擎,但是我不太喜歡這種方式,因為我覺得html本身的語義特性是我喜歡的,另外html本身也足夠簡潔,使用html自身做為模板語言更符合我的喜好,所以我選擇ejs。
歡迎訪問玄魂的博客
在網站根目錄啟動控制台,輸入
npm install ejs
程序包會安裝到node_modules目錄下。
修改app.js 將view engine修改為ejs。
在views文件夾中創建index.html,內容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pivotal Search</title>
<meta name="description" content="">
<meta name="author" content="">
<!-- HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- styles -->
<link href="stylesheets/style.css" rel="stylesheet">
</head>
<body>
<%= title %>
</body>
</html>
然后在router里修改index.js:
exports.index = function(req, res){
res.render('index', { title: '玄魂的測試代碼' });
};
運行結果:
原文鏈接:
在express站點中使用ejs模板引擎
ps:對此文章感興趣的讀者,可以加qq群:Hacking:303242737;Hacking-2群:147098303;Hacking-3群:31371755;hacking-4群:201891680;Hacking-5群:316885176