【问题标题】:Using an array with data to make elements using an iterative #each with a template使用带有数据的数组使用带有模板的迭代 #each 来制作元素
【发布时间】:2019-12-06 10:25:54
【问题描述】:

我有这个“帖子”模板:

<section class="posts">
<div class="post" data-price="{{price}}" data-city="{{city}}" data-condition="{{condition}}">
  <div class="post-contents">
    <div class="post-image-container">
      <img src="{{url}}" alt="{{name}}">
    </div>
    <div class="post-info-container">
      <a href="#" class="post-title">{{name}}</a> <span class="post-price">${{price}}</span> <span class="post-city">({{city}})</span>
    </div>
  </div>
</div>
</section>

“postData.json”中的数据数组如下所示:

[
  {
    "price": 500,
    "city": "Eugene",
    "condition": "poor",
    "photoURL": "(big url)",
    "description": "Super nice laptop"
  }]

我正在尝试将此数组中的数据获取到模板和我的正文中,但我不确定如何对其进行迭代并将该数据传输到模板和我的正文中,但这就是我所拥有的:

<section id="posts">
    {{#each postData}}
      {{> post}}
      {{
          name: allPosts.description;
          city: allPosts.city
          url: allPosts.photoURL
          condition: allPosts.conditions
          price: allPosts.price]
      }}
    {{/each}}
  </section>

让我知道任何问题,只是真的卡住了,无法弄清楚。感谢您的帮助,一切都有帮助!

【问题讨论】:

    标签: javascript html express handlebars.js express-handlebars


    【解决方案1】:

    当您对数据进行迭代时,您需要通过将代码更新为类似于 {{&gt; post this}} 的方式将当前数据集提供给您的部分数据。现在你的部分没有被传递任何数据。

    Handlesbars Partials documentation.

    【讨论】:

      猜你喜欢
      • 2012-01-20
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多