javascript - Nunjucks not rendering template - Stack Overflow

I have folder structure as follows:app|-routes.jspublic |-index.htmlserver.jsServer.js looks like t

I have folder structure as follows:

/app
   |-routes.js
/public 
   |-index.html
server.js

Server.js looks like this:

var nunjucks = require('nunjucks');
var express        = require('express');
var app            = express();
nunjucks.configure('public', { autoescape: true, express: app });
var port = process.env.PORT || 8080; 
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app); // configure our routes
app.listen(port);               
exports = module.exports = app;

app/routes.js looks like this:

module.exports = function(app) {
        app.get('*', function(req, res) {
            res.render('index.html', { awesome: 'page-title' }); 
        });
    };

public/index.html looks like this:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <base href="/">

    <title>{{awesome}}</title>

</head>
<body>
        <h1>{{ awesome }}</h1>    
    </div>
</body>
</html>

When I start up my node app and browse to localhost:8080, the page title is the string {{ awesome }} and the contains the string {{ awesome }} rather than the required "page-title". How e nunjucks is not rendering the variable into the template?

I have folder structure as follows:

/app
   |-routes.js
/public 
   |-index.html
server.js

Server.js looks like this:

var nunjucks = require('nunjucks');
var express        = require('express');
var app            = express();
nunjucks.configure('public', { autoescape: true, express: app });
var port = process.env.PORT || 8080; 
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app); // configure our routes
app.listen(port);               
exports = module.exports = app;

app/routes.js looks like this:

module.exports = function(app) {
        app.get('*', function(req, res) {
            res.render('index.html', { awesome: 'page-title' }); 
        });
    };

public/index.html looks like this:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <base href="/">

    <title>{{awesome}}</title>

</head>
<body>
        <h1>{{ awesome }}</h1>    
    </div>
</body>
</html>

When I start up my node app and browse to localhost:8080, the page title is the string {{ awesome }} and the contains the string {{ awesome }} rather than the required "page-title". How e nunjucks is not rendering the variable into the template?

Share Improve this question asked Nov 6, 2014 at 19:15 Jonathan SmithJonathan Smith 2,5991 gold badge37 silver badges67 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You have to split 'public' folder. What is in public, cannot be processed by nunjucks. It's static (you declared it so). Put your nunjucks template in folder e.g. 'views'

/app
   |-routes.js
/public 
   |css/
   |img/
/views
   |- index.html
server.js

nunjucks.configure('views', ...)

if you use thinkjs on windows: your view.js should look like this:

export default {type: 'nunjucks',root_path: think.ROOT_PATH + '\\view',};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745189897a4615817.html

相关推荐

  • javascript - Nunjucks not rendering template - Stack Overflow

    I have folder structure as follows:app|-routes.jspublic |-index.htmlserver.jsServer.js looks like t

    3小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信