<span style="font-family:KaiTi_GB2312;font-size:24px;">app.js
var http = require('http'),
swig = require('swig');
http.createServer(function (req, res) {
var tmpl = swig.compileFile(__dirname + '/template.html'),
renderedHtml = tmpl({
people: [
{ name: 'Paul', age: 28 },
{ name: 'Jane', age: 26 },
{ name: 'Jimmy', age: 45 }
],
title: 'Basic Example'
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(renderedHtml);
}).listen(1337);
console.log('Application Started on http://localhost:1337/');
template.html
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>{{ title }}</title>
</head>
<body>
<h1>{{ title }}</h1>
<ul>
{% for person in people %}
<li>{{ person.name }} age {{ person.age }}</li>
{% endfor %}
</ul>
</body>
</html>
</span>
swig简单应用实战
最新推荐文章于 2023-06-14 21:43:04 发布