Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to handle multiple "pages" in meteor

Tags:

meteor

What is the "formal" way of handling multiple "pages" in meteor? I say "pages" I've seen people do it a couple of different ways. I've seen people create actual full pages, (index.html, about.html, contact.html) and then when links are clicked, you'd write a route to render those pages. But I've also seen people essentially put the code for each of those pages inside <template> tags and then do nifty show/hide type stuff based of what they've clicked, login credentials, etc.

like image 990
Joshua Terrill Avatar asked Nov 15 '14 08:11

Joshua Terrill


1 Answers

There are several ways to handle multiple pages in meteor

1.iron-router

using iron router you can create a layout template and inject all other templates inside it using {{> yield}}. Check the iron-router guide to know more about layout template.

2.{{> Template.dynamic}}

if you do not want to add iron-router you can use {{> Template.dynamic}} to achieve the same.

<body>
  <ul>
    <li><a href="#" class="index">Home</li>
    <li><a href="#" class="about">About</li>
    <li><a href="#" class="contact">Contact</li>
  </ul>
  {{> Template.dynamic template=template_name }}
</body>

template_name can be changed reactively using a template helper

Meteor.startup(function () {
  Session.setDefault("templateName", "index")
});

Template.body.helpers({
  template_name: function(){
    return Session.get("templateName")
  }
});

Template.body.events({
  "click .home": function() {
    Session.set("templateName", "index");
  },
  "click .about": function() {
     Session.set("templateName", "about");
  }
  // ..
});

If the Session returns "about" then, {{> Template.dynamic template="about"}} which is equivalent to {{> about}}.

like image 200
Rajanand02 Avatar answered Nov 20 '22 19:11

Rajanand02