一尘不染

Node.js + Express + Hogan.js的部分

node.js

我正在使用Node.js + Express开发一个站点,并使用Hogan.js作为视图引擎。

这是我的文件app.js

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'hjs');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(app.router);
  app.use(require('less-middleware')({ src: __dirname + '/public' }));
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/about', routes.about);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

该文件/routes/index.js是:

/*
 * GET pages.
 */

exports.index = function(req, res){
  res.render(
    'index',
    {
      title: 'Home Page',
      author: 'Bruce Wayne'
    }
  );
};

exports.about = function(req, res){
  res.render(
    'about',
    {
      title: 'About Page',
      author: 'Bruce Wayne'
    }
  );
};

/views文件夹中,有:

|- part.hjs

|- index.hjs

|- cv.hjs

该文件part.hjs是:

<h3>Hello {{ author }}</h3>

该文件index.hjs是:

<h1>Title: {{ title }} </h1>
{{> part }}
Welcome to Gotham City.

文件about.hjs是:

<h1>Title: {{ title }}</h1>
{{> part }}
I'm not Joker.

我有两个问题:

  1. 如何正确使用页面中的部分内容?(此代码无效)
  2. 我可以在两个或多个页面上使用相同的“标题”而不重复文件中的值分配/routes/index.js吗?

最好的问候,六。


阅读 305

收藏
2020-07-07

共1个答案

一尘不染

我找到了第一个问题的解决方案。

首先,我删除了hjs

npm remove hjs

然后,我安装了软件包hogan-express

npm install hogan-express

此外,我编辑了app.js

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

var app = express();

app.engine('html', require('hogan-express'));
app.enable('view cache');

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'html');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(app.router);
  app.use(require('less-middleware')({ src: __dirname + '/public' }));
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

routes/index.js

exports.index = function(req, res) {
  res.locals = {
    title: 'Title',
  };
  return res.render(
    'index',
    {
      partials:
      {
        part: 'part',
      }
    }
  );
};

现在,/viewsindex.htmlpart.html。该文件part.html包含:

<h1>{{ title }}</h1>

该文件index.html包含:

{{> part}}
Hello world!

因此,它工作正常。

2020-07-07