如何从自定义句柄助手访问请求对象 [英] How to get access to request object from custom handlebars helper

查看:103
本文介绍了如何从自定义句柄助手访问请求对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有node.js和express的handlebars,并且我有一个用于温度显示的自定义注册助手,我想从页面URL访问查询参数。

I'm using handlebars with node.js and express and I have a custom registered helper for temperature display that I'd like to have access to a query parameter from the page URL.

帮助程序背后的概念是自动根据是否?tempFormat = F tempFormat = C 是否在URL中。这里是我想要的自定义帮助器的伪代码:

The concept behind the helper is to handle Fahrenheit to Celsius conversion automatically based on whether ?tempFormat=F or tempFormat=C is in the URL or not. Here's the pseudo code for the custom helper I'd like to have:

hbs.registerHelper("formatTemp", function(temp) {
    if (query parameter says to use Fahrenheit) {
        temp = toFahrenheitStr(temp);
    }
    return temp;
});

所以,我想让我的模板看起来像这样:

So, I'd like my template to look like this:

{{#each temperatures}}
    <div class="row {{{stripes @index}}}"> 
        <div class="time">{{prettifyDate t}}</div>
        <div class="atticTemp">{{formatTemp atticTemp}}</div>
        <div class="outsideTemp">{{formatTemp outsideTemp}}</div>
        <div class="spacer"></div>
    </div>
{{/each}}






我现在通过拉 request.query.tempFormat 并将其放入给模板呈现的数据中来处理这个问题:


I'm working around the issue now by pulling request.query.tempFormat and putting it in the data given to the template rendering:

app.get('/debug', function(request, response) {
    var tempData = {
        temperatures: data.temperatures,
        units: request.query.tempFormat || "C"
    };
    response.render('debug', tempData);
});

然后将该数据传递给模板:

And, then passing that data in the template:

{{#each temperatures}}
    <div class="row {{{stripes @index}}}"> 
        <div class="time">{{prettifyDate t}}</div>
        <div class="atticTemp">{{formatTemp atticTemp ../units}}</div>
        <div class="outsideTemp">{{formatTemp outsideTemp ../units}}</div>
        <div class="spacer"></div>
    </div>
{{/each}}
< div class =time> {{prettifyDate t}}< / div>
< div class =atticTemp> {{formatTemp atticTemp ../units}}</div>
< div class =outsideTemp> {{formatTemp outsideTemp ../units}}</div>
< div class =spacer>< / div>
< / div>
{{/ each}}


But, this seems messy because I have to pass the temperature units to every template render call and I have to then put them in every template where I want them used by a temperature display. They're sitting there in the request object already. So, I'm trying to figure out how to get access to the request object from a handlebars custom helper function? That way, I could save code for every render and save code in each template and have the tempFormat query parameter just automatically apply to any use of formatTemp in my templates.






但是,这似乎很混乱,因为我必须将温度单位传递给每个模板渲染调用,然后我必须将它们放在每个我希望它们用于温度显示的模板中。他们已经坐在请求对象的那里。所以,我试图找出如何从handlebars自定义帮助函数访问请求对象?这样,我可以为每个渲染保存代码并将代码保存在每个模板中,并使 tempFormat 查询参数自动应用于任何使用 formatTemp 在我的模板中。

Does anyone know how to get access to the request object from a handlebars custom helper without using a global?

有谁知道如何从handlebars定制助手访问request对象而不使用全局吗?

解决方案

推荐答案

首先,您需要通过在路由之前的任何位置注册中间件来将请求对象分配给本地响应

app.use(function(req,res,next){ res.local.req = req; next(); })

Then you could access the query object in you helper
<div class="atticTemp">{{formatTemp atticTemp ../req.query.tempFormat }}</div>

这篇关于如何从自定义句柄助手访问请求对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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