简体   繁体   中英

How Do I Set Meteor Template Title On Render?

I have a dynamic line of code:

 <label>{{title}}</label>
//template name="header"

I'm using iron:router, the app is super simple right now:

Router.configure({
  layoutTemplate: 'ApplicationLayout'
})

Router.route('/', {
  template: "home"
})

Router.route('/scientific', {
  template: "scientific"
})

I'd like a solution that doesn't rely on Session as a way to dynamically render the {{title}} .

Ideally I'd like to define this title somewhere in my router code, and just have my header automatically pick it up. I don't particularly want to do a lot of Session.set/get over in my Template.rendered callbacks (I seem to get an issue doing this with Semantic-UI checkboxes).

Do you guys have any elegant, super simple solutions?

PS: the template 'header' is in the ApplicationLayout template. The ApplicationLayout has a {{> yield}} below the header.

A viable option is to store your title inside your route options like this :

Router.route("/",{
  template:"home",
  title:"Welcome Home !"
});

Router.route("/scientific",{
  template:"scientific",
  title:"Scientific stuff"
});

Then you can define a title helper on your header template based on the (reactive) current route controller.

Template.header.helpers({
  title:function(){
    return Router.current().route.options.title;
  }
});

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