在Blaze模板中访问对象的属性名称

Luk*_*uke 5 html javascript templates meteor meteor-blaze

说我有一个看起来像这样的帮手:

Template.profile.helpers({
  info: {
    Name: 'Bob Dinkleberg',
    Age: 45,
    Location: 'Earth, Milky Way'
  }
});
Run Code Online (Sandbox Code Playgroud)

我想把这个信息放在一个<ul>.这是一些伪代码:

<template name="profile>
  <ul>
    {{#each}}
      <li> {{key}}: {{property}} </li>
    {{/each}}
  </ul>
</template>
Run Code Online (Sandbox Code Playgroud)

请原谅我,如果这是微不足道的,我是Meteor和Blaze的新手,我无法在网上找到解决方案.

fuz*_*nny 2

这会很有帮助:

http://meteorcapture.com/spacebars/

你想用{{#with}}.

<template name="profile">
  <ul>
    {{#with info}}
      <li>{{Name}}</li>
      <li>{{Age}}</li>
      <li>{{Location}}</li>
    {{/with}}
  </ul>
</template>
Run Code Online (Sandbox Code Playgroud)

虽然您的帮助代码是正确的:

Template.profile.helpers({
  info: {
    Name: 'Bob Dinkleberg',
    Age: 45,
    Location: 'Earth, Milky Way'
  }
});
Run Code Online (Sandbox Code Playgroud)

我个人喜欢养成将助手的名称映射到返回某些内容的函数的习惯。

Template.profile.helpers({

  info: function(){

    return {
      Name: 'Bob Dinkleberg',
      Age: 45,
      Location: 'Earth, Milky Way'
    };

  }

});
Run Code Online (Sandbox Code Playgroud)

编辑

Template.content.helpers({

  info: function(){
    var obj = {
      Name: 'Bob Dinkleberg',
      Age: 45,
      Location: 'Earth, Milky Way'
    };
    var arrayOfObjects = [];

    // creating an array of objects
    for (key in obj){
      arrayOfObjects.push({key: key, value: obj[key]});
    };
    console.log("This is the array of objects: ", arrayOfObjects);
    return arrayOfObjects;
  },

});
Run Code Online (Sandbox Code Playgroud)

HTML:

{{#each info}}
    {{value}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)