3

我对 swig 模板和 node.js 有疑问。

我的代码:

nodejs_swig.js 文件的路径:/node

nodejs_swig.js: // __dirname 是 /node

var http = require('http'),
    swig = require(__dirname + '/node_modules/swig');

swig.init({
    root: '/web/public/',
    allowErrors: true
});
console.log(swig);
http.createServer(function (req, res) {
    var tmpl = swig.compileFile('page.html'),
        renderedHtml = tmpl.render({
            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/');

html 文件在这里:/web/public

page.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>

当我想在链接 ip/page.html 上查看结果时,我得到:

{{ pagename|title }}

{% for author in authors %} {{ author }} {% else %}
There are no authors.
{% endfor %}

这是我在屏幕上看到的输出。

有谁知道出了什么问题?

提前致谢,

对不起我的英语不好

4

0 回答 0