nodeclub 源码问题:ejs模板 引用外部js变量
发布于 9 年前 作者 bingqingwu 3569 次浏览 最后一次编辑是 8 年前 来自 问答

nodeclub 使用如下方式: app.set(‘views’, path.join(__dirname, ‘views’)); app.set(‘view engine’, ‘html’); app.engine(‘html’, require(‘ejs-mate’)); app.locals._layoutFile = ‘layout.html’;

在layout.html 文件中: <meta name=‘description’ content=’<%= config.description %>’> config.description 是config.js 中定义的变量.

我的使用如下: app.set(‘views’, path.join(__dirname, ‘views’)); app.set(‘view engine’, ‘ejs’); 在index.ejs 文件中: <meta name=‘description’ content=’<%= config.description %>’> 但是web页面却显示 config 没有定义, 我应该怎么使用config.js

2 回复

app.locals.config = config;

这样在render 页面中就可以直接使用 config

推荐你了解下 view-helpers 这个库

@jerrywu55 我在app.js 中看到一些信息,也是可以用的,跟你这个应该原理一样 var _ = require(‘lodash’); _.extend(app.locals, { config: config, Loader: Loader, assets: assets }); 对于模块lodash, 一直不知道作用有哪些,可以介绍下吗?

回到顶部