【问题标题】:ejs <%- body %> not workingejs <%- body %> 不工作
【发布时间】:2025-12-25 20:55:12
【问题描述】:

我将使用一个名为 layout.ejs 的默认模板,这是文件中的代码:

<html>
<head>
    <title></title>
</head>
<body>
    <%- body %>
</body>
</html>

现在我正在尝试将此文件与我的 new.ejs 绑定,它位于我的事件文件夹中:

<% if (parseInt(page['prevPage'].charAt(0)) === 0) { %>
  <a type="button" class="btn btn-default" href="/admin/bookings/<%= page.nextPage %>">Próximo &gt;</a>
<% } else { %>
  <a type="button" class="btn btn-default" href="/admin/bookings/<%= page.prevPage %>">&lt; Anterior</a>
  <a type="button" class="btn btn-default" href="/admin/bookings/<%= page.nextPage %>">Próximo &gt;</a>
<% } %>

模板引擎不工作,没有绑定两个文件。我需要在 express 中进行一些额外的配置吗?

我正在使用 express 4.*

谢谢。

【问题讨论】:

  • 你放了吗:app.set('view engine', 'ejs');在您的快速配置中?
  • 是的@Ricoxor,我的应用程序处于开发的最后一步,我现在只需要配置布局模板
  • 请粘贴更多代码!我帮不了你:/你想在正文中显示什么? scotch.io/tutorials/use-ejs-to-template-your-node-application

标签: javascript node.js express ejs


【解决方案1】:

express 4.x 不支持布局,你可以使用模块“express-ejs-layouts”来解决这个问题

npm install express-ejs-layouts

在您的代码中,请包含:

var expressLayouts=require("express-ejs-layouts");
app.use(expressLayouts);

【讨论】:

    【解决方案2】:

    这是我过去在 Express 4 中使用 ejs 布局的猴子补丁:

    /*
       Usage:
         Set a global/default layout with:
            app.set('view layout', 'foo');
         Set a layout per-render (overrides global layout) with:
            res.render('foo', { layout: 'bar' });
         Or disable a layout if a global layout is set with:
            res.render('foo', { layout: false });
         If no layout is provided using either of the above methods,
         then the view will be rendered as-is like normal.
    
         Inside your layout, the variable `body` holds the rendered partial/child view.
    
       Installation:
         Call `mpResponse();` before doing `require('express');` in your application.
    */
    
    function mpResponse() {
      var expressResponse = require('express/lib/response'),
          expressResRender = expressResponse.render;
      expressResponse.render = function(view, options, fn) {
        options = options || {};
        var self = this,
            req = this.req,
            app = req.app,
            layout,
            cb;
    
        // support callback function as second arg
        if (typeof options === 'function')
          fn = options, options = {};
    
        // merge res.locals
        options._locals = self.locals;
    
        // default callback to respond
        fn = fn || function(err, str) {
          if (err) return req.next(err);
          self.send(str);
        };
    
        if (typeof options.layout === 'string')
          layout = options.layout;
        else if (options.layout !== false
                 && typeof app.get('view layout') === 'string')
          layout = app.get('view layout');
    
        if (layout) {
          cb = function(err, str) {
            if (err) return req.next(err);
            options.body = str;
            expressResRender.call(self, layout, options, fn);
          };
        } else
          cb = fn;
    
        // render
        app.render(view, options, cb);
      };
    }
    

    【讨论】:

    • Express 没有明确支持它,但是一些模板引擎自己支持它(虽然不是 ejs)并且可能会寻找 layout 和其他类似的视图变量。