meteorhacks:ssr

v2.2.0Published 9 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Build Status

Server Side Rendering for Meteor

Now, you can render Blaze templates on the server very easily. And also, you can assign helpers for templates in the server as well.

Previously, this package loads all the client side templates when your app starts. But now meteor don't allow us to do that. So that behaviour has been removed.

Installation

meteor add meteorhacks:ssr

Usage

This package only works on the server

First create templates and helpers

1SSR.compileTemplate('emailText', 'Hello {{username}}, <br> Now time is: {{time}}');
2
3Template.emailText.helpers({
4  time: function() {
5    return new Date().toString();
6  }
7});

Then you can render above template anywhere in your app.(only on server)

1Meteor.methods({
2  sendEmail: function() {
3    var html = SSR.render("emailText", {username: "arunoda"});
4    console.log(html);
5  }
6});

Better way to load templates

It's not a good idea to write template(html) inside javascript. So, we can use following approach.

Write your html content inside the private directory.

1<!-- file: private/hello.html -->
2Hello {{username}}, <br>
3Now time is: {{time}}

Then load it like this:

1SSR.compileTemplate('emailText', Assets.getText('hello.html'));
2
3Template.emailText.helpers({
4  time: function() {
5    return new Date().toString();
6  }
7});

You can render the template as previously.

API

SSR.render(template, data)

You can render a template with data. For template argument you can either pass the name of the template or the actual template instance itself.

SSR.compileTemplate(templateName, stringTemplateContent, [options])

You can use this API to compile templates in the server. The options parameter allows you to choose the template language with the language option.

If not provided this default to html which is handled by the spacebars compiler. You can also use jade as another option — and in this case you need to add following package:

meteor add mquandalle:jade

Note: the order in which you add jade and SSR matters! First add jade as a dependency and then SSR, otherwise the jade-compiler can not be located by Meteor.

What can we do with SSR

Since, this is full Blaze on the server, you can have sub-templates, dynamic templates and all the awesome features of Blaze. These are the few things you can do with SSR.

  • Render HTML pages for SEO bots
  • Render HTML pages for some of your routes (you may need to serve html yourself)
  • Build SEO aware static sites
  • Handy email templates with Blaze

Wow, How you did this

Actually, most of the stuff has been already done by Meteor, so kudos to Meteor team.

  • This also, adds some patches to Blaze.
  • Finally, this package comes with a clean and nice API to render templates.