如何将get请求中的变量传递到部分Handlebars页面

时间:2017-05-13 10:42:49

标签: javascript session get handlebars.js session-variables

我可以从登录中获取会话变量id并将其设置为id,但我不确定如何在页面加载到句柄页面时传递变量。

我注释掉res.send我只是想看看是否会加载emailid,它确实加载了。我已经查看了一些示例,但仍不确定如何将变量直接传递到handlebars / html中的{{username}}点。

<!--Where the city and state info will be inputted -->
<div class="row">  
  <div class="col-sm-3">  
    <form>  
      <p>Search for parking</p>   
      <input id="location-input" type="textbox" placeholder="Enter a location" />
      <br />
      <br />
<!--When Clicked will push all the selected information above to GET API responses -->         
      <button id="submit-button" value="submit">Submit</button>  
    </form>
      <br />
      <br />
      <div id="test">
        Currently Logged in as: <br /> {{username}}
      </div>
      <br /> 
  </div>

// goes to the application folder and checks to see if authenticated
	app.get("/user", application.IsAuthenticated, function(req, res) {
// to help organize, some of the handlebars are within folders 		
		var username = req.user.username;
		// res.send(username);
		res.render('./users/user');
	});

1 个答案:

答案 0 :(得分:0)

使用res.render秒参数将数据传递到视图:

res.render('./users/user', { username: username });

来自docs

res.render(view [,locals] [,callback])

呈现view并将呈现的HTML字符串发送到客户端。可选参数:

  • locals,一个对象,其属性定义了局部变量 图。
  • 回调,一个回调函数。如果提供,则该方法返回两者 可能的错误和渲染的字符串,但不执行 自动回复。发生错误时,该方法将调用 下一个(错误)内部