简体   繁体   English

如何在 Express.js 布局中使用局部?

[英]How to use partials in Express.js layout?

I have a layout.ejs file that contains my site's basic boilerplate HTML markup: doctype declaration, head, body, footer, the basics...我有一个 layout.ejs 文件,其中包含我网站的基本样板 HTML 标记:doctype 声明、head、body、footer、基础知识...

How would I go about placing the navigation in a separate partial file and including it into this layout?我将如何将导航放在单独的部分文件中并将其包含在此布局中? Is there a particular require() or include() function for doing this?是否有特定的 require() 或 include() 函数来执行此操作?

I am using EJS view engine.我正在使用 EJS 视图引擎。

I came across similar issue with handlebars template, working with expressjs 4.0我在使用 expressjs 4.0 时遇到了与把手模板类似的问题

In my app.js:在我的 app.js 中:

var hbs = require('hbs');

// register path to partials
hbs.registerPartials(__dirname + '/views/partials');

Then add a partial file to your partials dir:然后将部分文件添加到您的部分目录:

/views/partials/nav.hbs

You could then call it within eg index.hbs like so:然后您可以在例如 index.hbs 中调用它,如下所示:

<!DOCTYPE html>
<html>
  <head>
    ...
  </head>

  <body>
    {{> nav}}
    ...
  </body>
</html>

Yes.是的。

<% include path/to/template %>

Documentation here.文档在这里。 https://github.com/visionmedia/ejs#includes https://github.com/visionmedia/ejs#includes

var hbs = require('express-handlebars');
// view engine setup
app.set('view engine', 'hbs');

app.engine( 'hbs', hbs( {
  extname: 'hbs',
  defaultView: 'default',
  layoutsDir: __dirname + '/views/layouts/',
  partialsDir: __dirname + '/views/partials/'
}));

see: page templates请参阅: 页面模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM