简体   繁体   中英

Template syntax in Meteor.js is not recognized?

First off, I want to mention that I don't have to much experience with Meteor.js, only starting a while ago, and not using it frequently. So please forgive me if this question seems stupid or obvious.

I am attempting to access an {{#each}} helper in my template (the template name is directory) :

{{#each teachers}}

    <h1>{{name}}</h1><br />
    <p>{{description}}</p>

{{/each}}

So to reference this, I typed in the following code :

Template.directory.teachers = function(){

}

But Meteor is throwing the following exception : "ReferenceError: Template is not defined"

Anybody know what I'm doing wrong here? Again, sorry if the answer is obvious. I am running the latest windows build, and all my packages are updated. Thanks for helping :)

Your directory.html file should look similar to this:

<template name="directory">
    {{#each teachers}}
        <h1>{{name}}</h1><br />
        <p>{{description}}</p>
    {{/each}}
</template>

Your directory.js file should look similar to this:

Template.directory.helpers({
  teachers: function () {
    return Teachers.find({}); // or as in your sample ['teacher1', 'teacher2'];
  }
});

The problem with this error is that you are trying to invoke the Template object on the server. Templates should exist only on the client. So you can wrap your code like this:

if(Meteor.isClient){
Template.directory.helpers({
  teachers: function() {
    return ['teacher1', 'teacher2']; //...
  }
});
}

That is not how you define helpers. Helpers are defined by passing objects to the helpers function

Template.directory.helpers({
  teachers: function() {
    return ['teacher1', 'teacher2']; //...
  }
});

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