javascript - Express EJS 布局 : res. 局部变量未在布局中呈现

标签 javascript node.js templates express ejs

我正在使用 Express EJS Layouts带有 Express 4 的模块。

我不确定如何标记它,但我遇到的问题是这个。我有一个路由文件、一个布局和一个 View 。我正在尝试使用以下代码将页面标题页面描述 传递到布局。

路线

router.get('/', function(req, res, next) {

  res.locals.meta = {
    title: 'Page Title',
    description: 'Page Description'
  };

  res.render('pages/index', {
    header: 'Page Header'
  });
});

布局

<!DOCTYPE html>
<html dir="ltr" lang="en" class="no-js">
<head>
  <title><%= meta.title %></title>
  <meta name="description" content="<%= meta.description %>">
</head>
<body>
  <%- body %>
</body>
</html>

查看

<h1 class="page-title"><%= header %></h1>

标题 在 View 中呈现,但它缺少页面标题页面描述。如果我将其他项目添加到 res.locals.meta 对象并在 View 中引用它们,它们将被渲染,但布局中的任何内容都不会被渲染。

我做错了什么?

非常感谢所有帮助。

最佳答案

字段 meta 总是在 express-ejs-layout 中设置,不管设置 the option to extract meta tags .

现在是fixed , 所以更新到最新版本的 express-ejs-layout 来解决这个问题。

关于javascript - Express EJS 布局 : res. 局部变量未在布局中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38353629/

相关文章:

javascript - 在 C# 端动态更改脚本标记源

javascript - 以优雅的方式根据模型的属性迭代 Backbone 集合

javascript - NodeJS : Send HTTPS request but get HTTP

c++ - 如何向 C++ 应用程序添加反射?

c++ - 使用模板函数请求 int 时调用 stoi

c# - 在 C# 中对多个类进行别名

javascript - 基于正则表达式用新的 DOM 节点替换 TextNode 中的文本

node.js - 如何安装 Jasmine Node (理想情况下不使用 'sudo')?

node.js - 如何在 node.js 服务器端重现 Backbonejs 模型和集合

php - 使用 jquery-ajax 从 url 获取 json 或 xml 数据时出现错误