简体   繁体   English

使用#each 显示handlebars.js 中的一些数据不显示

[英]Using #each to display some data in handlebars.js not displaying

Hi have a project where i'm converting a static website into a datadriven one, for now i am just trying to change so the content of each page is taken into the website through a data object.嗨,有一个项目,我正在将静态网站转换为数据驱动的网站,现在我只是尝试更改,以便通过数据对象将每个页面的内容带入网站。

I'm using : node.js, handlebars, express我正在使用:node.js、把手、快递

My index.js file has an object like this我的 index.js 文件有一个这样的对象

var item = [{
  pagetitle: 'Page 1',
  text: 'content page 1'
}, {
  pagetitle: 'Page 2',
  text: 'content page 2'
}, {
  pagetitle: 'Page 3',
  text: 'content page 3'
}, {
  pagetitle: 'Page 4',
  text: 'content page 4'
}, {
  pagetitle: 'Page 10',
  text: 'content page 10'
}];

my index.hbs looks like this我的 index.hbs 看起来像这样

<div class="bb-blah">
  {{title}
  {{#each item}}
  <div class="bb-item" id="item{{@key}}">
     <h2>{{pagetitle}}</h2>
     <div class="cols-2">
         <p>{{text}}</p>
     </div>
  </div>
  {{/each}}
</div>

For some reason nothing is displayed on the page like it should be.出于某种原因,页面上没有显示应有的内容。 "item" should iterate item1 item2 item3 to display each page of the website so i need that to increment every time i press my button (this works on the normal website but i can't get it to work with @key. Also the {{pagetitle}} and {{text}} do not display at all on the website. “item”应该迭代 item1 item2 item3 以显示网站的每个页面,所以我每次按下按钮时都需要增加它(这适用于普通网站,但我无法使用@key。还有 { {pagetitle}} 和 {{text}} 根本不显示在网站上。

It looks like you need to specify what the item property is on the object that you are passing to the template in order for the template to iterate over the items in the item array.看起来您需要指定传递给模板的对象上的item属性是什么,以便模板迭代item数组中的item

When you are rendering the template, pass in the item array under the property of item .当你渲染模板,通过在item的属性下阵列item In doing so, your template will see the item property and it will iterate over the items as expected.这样做时,您的模板将看到item属性,它将按预期迭代项目。

var template = Handlebars.compile(html);
var rendered = template({
  item: item
});

The reason there was likely some confusion was because you probably expected the handlebars template to render the item variable, despite not being a property on the object that was passed in.可能存在一些混淆的原因是因为您可能希望把手模板呈现item变量,尽管它不是传入的对象的属性。

Here is a basic example demonstrating this:这是一个基本示例,演示了这一点:

 var html = $("#template").html(); var template = Handlebars.compile(html); var item = [{pagetitle : 'Page 1', text:'content page 1'}, {pagetitle : 'Page 2', text:'content page 2'}, {pagetitle : 'Page 3', text:'content page 3'}, {pagetitle : 'Page 4', text:'content page 4'}, {pagetitle : 'Page 10', text:'content page 10'}]; $('body').append(template({item: item}));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script id="template" type="text/x-handlebars-template"> {{#each item}} <div class="bb-item" id="item{{@key}}"> <h2>{{pagetitle}}</h2> <div class="cols-2"> <p>{{text}}</p> </div> </div> {{/each}} </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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