Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get access to request object from custom handlebars helper

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.

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}}

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}}

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.

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

like image 938
jfriend00 Avatar asked Sep 01 '14 18:09

jfriend00


People also ask

How do you use handlebars helpers?

The with helper Implementing a helper like this is a lot like implementing the noop helper. Helpers can take parameters, and parameters are evaluated just like expressions used directly inside {{mustache}} blocks. Handlebars. registerHelper("with", function(context, options) { return options.

What are helpers in handlebar?

Helpers are the proposed way to add custom logic to templates. You can write any helper and use it in a sub-expression. For example, in checking for initialization of a variable the built-in #if check might not be appropriate as it returns false for empty collections (see Utils. isEmpty).

How do you comment code on HBS?

Template comments If you'd like the comments to show up just use HTML comments, and they will be output. Any comments that must contain }} or other handlebars tokens should use the {{!-- --}} syntax.


1 Answers

First you need to assign your request object to the response local by registing a middleware anywhere before your route

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>
like image 191
Roland Avatar answered Sep 20 '22 10:09

Roland