require an external module in jade in a node.js webserver

喜夏-厌秋 提交于 2020-01-04 04:38:06

问题


I need to require a util module in my jade template to do some checking.

Can I do that? I tried following in a jade template which sits in $ROOT/views/jade/sample.jade

var utils = require('../../app/server/modules/queries.js')

for a module that sits in

$ROOT/app/server/modules/queries.js

But it does not work.

Can I do what I want????


回答1:


You can register helpers from within Express.

In a request handler.

var utils = require('../../app/server/modules/queries.js')
function(req, res) {
  res.render("sample", {
    locals: {
      title: "Welcome to Derpco",
      someUtilFunction: utils.someUtilFunction
    }
  });
};

Also you can register helpers globally using app.locals.helpername = ...



来源:https://stackoverflow.com/questions/18407998/require-an-external-module-in-jade-in-a-node-js-webserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!