简体   繁体   中英

Meteor JS: RegisterHelper to display associated user information

I am working on a small MeteorJS application (for personal use) that will be an extremely basic CMS.

I am wanting to display the User information next to each post (the user who created the post).

This is my handlebars (or spacebars) template:

<template name="post">
  <div class="row">
    <div class="col-md-7">
      <h3>{{ title }} - <span class="time" title="{{createdAt}}">{{createdAt}}</span></h3>
      <p>Created By - {{ user_id }}</p>
      {{{content}}}
    </div>

    <div class="col-md-5">
      {{> editActions }}
    </div>
  </div>
</template>

<template name="editActions">
  <ul class="nav nav-pills">
    <li>
      <a href="{{ pathFor 'postEdit' }}">
        <i class="glyphicon glyphicon-edit"></i>
      </a>
    </li>
    <li>
      <a href="{{ pathFor 'postRemove' }}" data-id="{{ _id }}" class="remove-post">
        <i class="glyphicon glyphicon-remove"></i>
      </a>
    </li>
  </ul>
</template>

I have the {{ user_id }} displaying, but I am not sure how to display the actual user email (or any other user information for that matter).

user_id is a field on my Posts collection.

Replace {{user_id}} with {{authorEmail}} and then add a template helper like this:

Template.post.helpers({
  authorEmail: function() {
    return Meteor.users.findOne(this.user_id).emails[0].address;
  }
});

If you want direct access to the author in your template, you can return the user document from your helper:

Template.post.helpers({
  author: function() {
    return Meteor.users.findOne(this.user_id);
  }
});

And then use it like so:

<div class="col-md-7">
  <h3>{{ title }} - <span class="time" title="{{createdAt}}">{{createdAt}}</span></h3>
  {{#with author}}
    <p>Created By - {{profile.name}}</p>
  {{/with}}
  {{{content}}}
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM