Trần Kim Dự
Trần Kim Dự

Reputation: 6112

Meteor: insert template between other templates and transfer data between templates

I'm using Meteor for creating web application. I have defined my layout is:

<template name="default_layout">
   {{> header}}
   body code here
   {{> footer}}
</template>

And here is my routing file:

Router.configure({ layoutTemplate: 'default_layout'
});
Router.map(function() { this.route('post_list', {path: '/'});
});

So. I have two questions:

  1. How to make template post_list go into body code of default_layout template ?
  2. Base on each layout for mapping page, maybe header and footer change content respectively. So, how to contact between template? For example, post_list template will set some value for header template ...

Thanks :)

Upvotes: 0

Views: 126

Answers (1)

Ramsay Lanier
Ramsay Lanier

Reputation: 426

  1. Use the {{> yield}} helper. This will insert whatever template your route is serving. So.

    <template name="default_layout">
       {{> header}}
       {{> yield}}
       {{> footer}}
    </template>
    
  2. If you want to change what goes into the header, you will be using "yield regions.

    <template name="default_layout">
       {{> yield "header"}}
       {{> yield
       {{> yield "footer"}}
    </template>
    

    Then, in, say, a route controll you could do this:

    PostController = RouteController.extend({
         yieldRegions: {
             'postHeader': {to: 'header'},
             'postFooter': {to: 'footer'}
         }
     })
    

    Then in your post_list route, do this:

    Router.route('post_list', function(){
        controller: 'postController'
    });
    

    Basically, you are creating a controller that can be re-used for certain routes, and telling the controller to put a template called "postHeader" into the {{> yield "header"}} region and "postFooter" into the {{> yield "footer"}} region.

Upvotes: 2

Related Questions