在Blaze模板中访问对象的属性名称 [英] Access an object's property names in a Blaze template

查看:341
本文介绍了在Blaze模板中访问对象的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Template.profile.helpers({
info: {
名称:'Bob Dinkleberg',
年龄:45,
地点:'地球,银河'
}
});

我想将这些信息放在< ul> 。这里有一些伪代码:

 < template name =profile> 
< ul>
{{#each}}
< li> {{key}}:{{property}}< / li>
{{/每个}}
< / ul>
< / template>

如果这很琐碎,请原谅我,我是流星新手和Blaze,我一直无法在网上找到解决方案。

解决方案

这会很有帮助:



http://meteorcapture.com/spacebars/



您想要使用 {{#with}}



<$ p $ lt; code>< template name =profile>
< ul>
{{with info}}
< li> {{Name }}< / li>
< li> {{年龄}}< / li>
< li> {{Location}}< / li& with}}


虽然你的帮手代码是正确的:

  Template.profile.helpers({
info:{
名称:'Bob Dinkleberg',
年龄:45 ,
地点:'地球,银河'
}
});

我个人喜欢养成将helper的名称映射到返回值的函数的习惯。

  Template.profile.helpers({

info:function(){

return {
名称:'Bob Dinkleberg',
年龄:45,
位置:'地球,银河'
};

}

});

编辑

< $ {

info:function(){
var obj = {
名称:'Bob Dinkleberg' ,
年龄:45,
位置:'Earth,Milky Way'
};
var arrayOfObjects = [];

//创建一个数组(key in obj){
arrayOfObjects.push({key:key,value:obj [key]});
};
console.log(这是对象数组:,arrayOfObjects);
return arrayOfObjects;
},

});

HTML:

  {{#each info}} 
{{value}}
{{/ each}}


Say I have a helper that looks like this:

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

I want to put this info in a <ul>. Here is some pseudo-code:

<template name="profile>
  <ul>
    {{#each}}
      <li> {{key}}: {{property}} </li>
    {{/each}}
  </ul>
</template>

Forgive me if this is trivial, I am new to Meteor and Blaze, and I haven't been able to find a solution online.

解决方案

This will be helpful:

http://meteorcapture.com/spacebars/

You want to use {{#with}}.

<template name="profile">
  <ul>
    {{#with info}}
      <li>{{Name}}</li>
      <li>{{Age}}</li>
      <li>{{Location}}</li>
    {{/with}}
  </ul>
</template>

While your helper code is correct:

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

I personally like to make a habit of mapping the name of the helper to a function that returns something.

Template.profile.helpers({

  info: function(){

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

  }

});

EDIT

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;
  },

});

HTML:

{{#each info}}
    {{value}}
{{/each}}

这篇关于在Blaze模板中访问对象的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆