Express-Handlebars: Getting current URL query and passing query to a template? Express-Handlebars: Getting current URL query and passing query to a template? express express

Express-Handlebars: Getting current URL query and passing query to a template?


So I figured it out. Pretty silly. I was defining a scope within my template so it only had access to info variables.

{{#info}} <h1>{{header}}</h1> ...{{/info}}

The req variable sends a query field for me to use so I ended up not needing a helper function at all. You can look up more here all the way at the bottom.

app.get('/info', function(req, res) {  //set context of handlebars  res.render('info', {     info  : {        header : "View Title"     },     query : req.query  }}

Because of how Handlebars works, you can simply add a context to your render function. It will then be available.

The thing that was tripping me up was the scope though so in my handelbars template, I just simply turn the scope on and off.

{{#info}}  <h1>{{header}}</h1>  ...{{/info}}  {{#if query.country}}    <h2>Welcome to the {{query.country}}!</h2>  {{else}}    <h2>Welcome!</h2>  {{/if}}{{#info}}  ... continue{{/info}}

Hope this helps.