How to pass 2 values in .render() to Handelbars

问题内容:

I am trying to pass multiple values into res.render to be passed into my handlebars template. It seems to fulfil the condition but it doesn’t show the content.

I’m trying to show a special navbar and a users snippets (their data) if they’re logged in. Otherwise I want to display my normal navbar and all snippets (data from all users).

I’ve got it working if I just hand in a single value, but I can’t seem to hand in multiple values.

Express code:

 router.route('/')
.get(function (request, response) {
if (request.session.name) {
  Snippet.find({ user: request.session.name._id }, function (err, data) {
    if (err) {
      console.log(err)
    }
    data.forEach(function (element, err) {
      if (err) {
        console.log(err)
      }
    })
    let context = {
      snippets: data.map(function (snippet) {
        return {
          _id: snippet._id,
          user: snippet.user,
          title: snippet.title,
          body: snippet.body,
          createdAt: snippet.createdAt
        }
      })
    }
    response.render('home/index', {loggedIn: true, context: context}) //Here is the issue
  })
  }

Handlebars template:

<ul class="people_list">
    {{#if loggedIn}}
        {{!-- Logged in --}}
        {{#each snippets}}
            <h2><i class="star fa fa-star-o" aria-hidden="true"></i> {{title}}</h2>
            <h4>{{ createdAt }}</h4>
            <pre>
                <code class="language-javascript">{{body}}</code>
            </pre>
            <hr />
        {{/each}}
    {{else}}
        {{!-- Not logged in --}}
        {{#each snippets}}
            <h2><i class="star fa fa-star-o" aria-hidden="true"></i> {{title}}</h2>
            <h4>{{ createdAt }}</h4>
            <pre>
                <code class="language-javascript">{{body}}</code>
            </pre>
            <hr />
        {{/each}}
    {{/if}}
</ul>

问题评论:

    
Hi @sasuri, are you saying that it works when you pass in just one item (context) but not when you pass in the loggedIn variable as well?
    
Also, can you tell us what npm package you’re using? There are multiple express handlebars modules (express-handlebars, express-hbs, etc).
    
I’m using express-secure-handlebars. Yes it works only when passing(context)
– sasuri
31 mins ago

原文地址:

https://stackoverflow.com/questions/47754147/how-to-pass-2-values-in-render-to-handelbars

添加评论

友情链接:蝴蝶教程