Express.js查看“全局变量” [英] Express.js View "globals"

查看:439
本文介绍了Express.js查看“全局变量”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Express.js(在Node.js上),我知道您可以通过locals参数渲染具有自定义数据的视图。 ( res.render(template,{localals:{foo:bar}});





我看到视图选项,但这不是递归的,所以它取代了我设置的本地人,如果我使用任何本地人与我的模板。



这是我的用例:我想让它,使CSS / JS文件可以在每页的基础上添加,这是我的主要布局的一部分。问题是,如果我没有在每个渲染上显式设置这些数组,我会得到一个未定义的错误,所以在我的模板中,我总是要执行 typeof css!==undefined舞蹈。另外,我还有其他选择框选项列表,我不想明确地添加到我的每个表单。

解决方案

自从Express 3发布以来,可能会遇到这个问题的人,值得注意的是,dynamicHelpers方法已经不存在了。



相反,您可以使用app.locals函数,它作为一个对象,您可以存储值或函数,然后使它们可用于视图。例如: -

  //在你的app.js等
app.locals.title =我的应用程序;
app.locals({
version:3,
somefunction:function(){
returnfunction result;
}
});

//然后在你的模板中(这里使用玉模板显示)

= title
= version
= somefunction()

//输出

我的App
3
函数结果

如果您需要访问请求对象以从中提取信息,则可以编写一个简单的中间件函数并使用app.settings变量。



例如,如果您正在使用connect-flash向用户提供消息,您可能会这样做:

  app.use(function(req,res,next){
app.set('error',req.flash('error'));
next();
}) ;

您可以在模板中使用= settings.error访问错误消息。



这些主题在这里介绍,虽然稍稍简要: http ://expressjs.com/api.html#app.locals



更新:Express 4



app.locals 现在是一个简单的JavaScript对象,所以每个属性都必须逐个设置。

  app.locals.version = 3; 
app.locals.somefunction = function(){
returnfunction result;
}

res.locals 提供完全相同的功能,除了它应该用于特定于请求的数据,而不是应用程序范围的数据。用户对象或设置是常见的用例。

  res.locals.user = req.isAuthenticated()? req.user:null; 
res.locals.userSettings = {
backgroundColor:'fff'
}


I'm using Express.js (on Node.js) and I know that you can render a view with custom data via the "locals" parameter. (res.render("template", { locals: { foo: "bar" } });)

Is there any way to have "globals"? (ie. data that's accessible to every view)

I saw view options, but that isn't recursive, so it replaces the locals I set if I use any locals with my template.

This is my use case: I want to make it so that CSS/JS files can be added on a per-page basis, and that is part of my main layout. The problem is, if I don't explicitly set those arrays on every render, I get an undefined error, so in my template I always have to do the typeof css !== "undefined" dance. Additionally, I have other select box option lists that I don't want to have to explicitly add to each of my forms.

解决方案

It's worth noting for those who may have come across this question since the release of Express 3, that the method 'dynamicHelpers' no longer exists.

Instead you can use the app.locals function, which acts as an object you can store values or functions in, and then makes them available to views. For example:-

// In your app.js etc.
app.locals.title = "My App";
app.locals({
    version: 3,
    somefunction: function() {
        return "function result";
    }
});

// Then in your templates (shown here using a jade template)

=title
=version
=somefunction()  

// Will output

My App
3
function result

If you need access to the request object to pull information from, you can write a simple middle-ware function and use the app.settings variable.

For example, if you are using connect-flash to provide messages to your users, you might do something like this:

app.use(function(req, res, next) {
    app.set('error', req.flash('error'));
    next();
});

Which would give you access to the error message with =settings.error in your template.

These topics are covered here, albeit slightly briefly: http://expressjs.com/api.html#app.locals

Update: Express 4

app.locals is now a simple JavaScript Object, so every property has to be set one by one.

app.locals.version = 3;
app.locals.somefunction = function() {
    return "function result";
}

res.locals provides the exact same functionality, except it should be used for request-specific data rather than application-wide data. A user object or settings is a common use case.

res.locals.user = req.isAuthenticated() ? req.user : null;
res.locals.userSettings = {
    backgroundColor: 'fff'
}

这篇关于Express.js查看“全局变量”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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