官术网_书友最值得收藏!

Listing posts

Now that we have walked through all ways of using helpers and data, I want to introduce a block helper named {{#each}}, which we will probably find the most useful.

If we go through all the examples completed up to now, we can see that it is better to delete all the examples of data context from our home template, the examples.html file, and its examples.js JavaScript file so that we can continue to build our blog cleanly.

The next step is to add a list of blog entries to our home page. For this, we need to create a template for a post preview. This can be done by performing the following steps:

  1. We create a file called postInList.html in our my-meteor-blog/client/templates folder and save it with the following code snippet:
    <template name="postInList">
      <div class="postListItem">
        <h2><a href="#">{{title}}</a></h2>
        <p>{{description}}</p>
        <div class="footer">
          Posted by {{author}}
        </div>
      </div>
    </template>

    This template will be used for each post we display in the home page.

  2. To make it appear, we need to add a {{#each}} helper to the home template, as follows:
    {{#each postsList}}
      {{> postInList}}
    {{/each}}

    When the postsList helper, which we pass to the {{#each}} block helper, returns an array, the content of {{#each}} will be repeated for each item in the array, setting the array item as the data context.

  3. To see this in action, we add the postsList helper in our home.js file to the template helpers, as follows:
    Template.home.helpers({
      // other helpers ...
      postsList: function(){
        return [
          {
            title: 'My Second entry',
            description: 'Borem sodum color sit amet, consetetur sadipscing elitr.',
            author: 'Fabian Vogelsteller',
            timeCreated: moment().subtract(3, 'days').unix()
          },
          {
            title: 'My First entry',
            description: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr.',
            author: 'Fabian Vogelsteller',
            timeCreated: moment().subtract(7, 'days').unix()
          }
        ];
      }
    });
  4. As we can see, we return an array where each item is an object containing our post's data context. For timeCreated, we use the moment function of our previously added third-party package. This will generate dummy timestamps of a few days in the past. If we now go to our browser, we will see the two posts listed, as shown in the following screenshot:
    Listing posts
  5. To display timeCreated from our post item in the correct format, we need to create a helper function to format the timestamp. However, because we want to use this helper in other templates later, we need to make it a global helper that can be accessed by any template. To do this, we create a file named template-helpers.js and save it to our my-meteor-blog/client folder, as it doesn't belonging to any specific template.
  6. To register a global helper, we can use Meteor's Template.registerHelper function:
    Template.registerHelper('formatTime', function(time, type){
      switch(type){
        case 'fromNow': 
          return moment.unix(time).fromNow();
        case 'iso':
          return moment.unix(time).toISOString();
        default:
          return moment.unix(time).format('LLLL');
      }
    });
  7. Now, we only have to add the helper to our postInList template by replacing the content of the footer with the following code snippet:
    <div class="footer">
      <time datetime="{{formatTime timeCreated "iso"}}">Posted {{formatTime timeCreated "fromNow"}} by {{author}}</time>
    </div>

Now, if we save both the files and go back to our browser, we will see a relative date added to our blog post's footer. This works because we pass the time and a type string to the helper, as follows:

{{formatTime timeCreated "fromNow"}}

The helper then returns the formatted date using a moment function.

With this global helper, we can now format any Unix timestamp, in any template to relative times, ISO time strings, and a standard date format (using the LLLL format, which converts to Thursday, September 4, 1986, 8:30 P.M.).

Now that we have already used the {{#with}} and {{#each}} block helpers, let's take a look at the other default helpers and syntax that Blaze uses.

主站蜘蛛池模板: 凉城县| 思南县| 高要市| 恭城| 黎城县| 格尔木市| 长子县| 泽库县| 临桂县| 永定县| 花莲县| 曲松县| 兴和县| 岳阳市| 中阳县| 蒙自县| 高安市| 疏附县| 丽水市| 琼海市| 通河县| 富阳市| 济南市| 浦北县| 福贡县| 喀喇沁旗| 绥化市| 澄城县| 阜康市| 那曲县| 灵台县| 榕江县| 米脂县| 日喀则市| 甘洛县| 视频| 襄汾县| 博客| 临夏市| 北碚区| 古丈县|