Express 3和EJS中的布局 [英] Layouts in Express 3 and EJS

查看:140
本文介绍了Express 3和EJS中的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Express的第3版中,某些功能被删除:

In version 3 of Express some features were removed:

the concept of a "layout" (template engine specific now)
partial() (template engine specific)

更改日志:https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to -3.x

可以更改EJS自己的功能<$ code> partial() c $ c> include ,但布局的替代方法是什么?

The partial() can be changed for EJS own feature called include, but what is the alternative for layouts?

推荐答案

Express 3,布局功能被委托给模板引擎的责任。您可以使用ejs-locals( https://github.com/RandomEtc/ejs-locals )进行布局。

It seems that from Express 3, layout feature is delegated to the responsibility of template engines. You can use ejs-locals (https://github.com/RandomEtc/ejs-locals) for layout.

安装ejs-locals

Install ejs-locals

npm install ejs-locals --save

在app.js中使用ejs-locals作为应用引擎

Use ejs-locals as your app engine in app.js

var express = require('express');
var engine = require('ejs-locals');
...

app.engine('ejs', engine);
app.set('view engine', 'ejs');

现在您可以使用布局

layout.ejs
<body>
  <%- body %>
</body>

index.ejs
<% layout('layout') -%>

<div class="container">
<div class="jumbotron">
...

另一个选项是使用express-partials(https://github.com/publicclass/express-partials )。这两个做同样的事情,所以这只是你的选择。

Another option is to use express-partials (https://github.com/publicclass/express-partials). The two do the same thing, so it's just your choice.

这篇关于Express 3和EJS中的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆