简体   繁体   中英

Handlebars.js grid with data

I'm using Parse.com to pull data from my server and display it in a handlebars.js template.

<script id="blogs-tpl" type="text/x-handlebars-template">
  {{#each blog}}

  <div class="blog-post">
    <p class="blog-post-meta"><a href="#"><img src="{{image.url}}"></a></p>
    <h5 id="productTitle" class="blog-post-title">{{productType}}</h5>
    <div>${{price}} USD</div>
    <div><span class="typcn typcn-heart" style="color: red; font-size: 20px;"></span> {{likesCount}}</div>
    <a href="" id="download-button" class="btn-large waves-effect waves-light indigo darken-2">Details</a>
  </div>


  {{/each}}
</script>

This is the js for communicating with the API:

var Blog = Parse.Object.extend("Blog");

var Blogs = Parse.Collection.extend({
    model: Blog
});

var blogs = new Blogs();

blogs.fetch({
    success: function(blogs) {
        var blogsView = new BlogsView({ collection: blogs });
        blogsView.render();
        $('.main-container').html(blogsView.el);
    },
    error: function(blogs, error) {
        console.log(error);
    }
});

var BlogsView =  Parse.View.extend({
    template: Handlebars.compile($('#blogs-tpl').html()),
    render: function(){ 
        var collection = { blog: this.collection.toJSON() };
        this.$el.html(this.template(collection));
    }
});

How can I display this in a 5xn (5 column) grid?

Heh. Figured it out after some experimentation so here's my answer. Given that I'm using the Materialize framework I can just use the m3 class to set the width of the column (and get a grid of 4 columns).

<script id="blogs-tpl" type="text/x-handlebars-template">

  <div class="blog-post">
    {{#each blog}}
    <div class="col s12 m3" style="overflow: hidden;">
      <div class="icon-block">
        <h2 class="center light-blue-text"></h2>

        <p class="blog-post-meta"><a href="#"><img style="width: 100%; height: auto;" src="{{image.url}}"></a></p>
        <h5 id="productTitle" class="blog-post-title">{{productType}}</h5>
        <div>${{price}} USD</div>
        <div><span class="typcn typcn-heart" style="color: red; font-size: 20px;"></span> {{likesCount}}</div>
        <a href="" id="download-button" class="btn-large waves-effect waves-light indigo darken-2">Details</a>

      </div>
    </div>

    {{/each}}
  </div>

</script>

I guess you can do the same thing with Bootstrap. The key is that #each appears outside of the div that determines the column width, ie col-sm-3, col-md-3, etc., so that you're repeating a 33.33% width column 3 three times, for example, before the next row is auto-generated.

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