Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MeteorJS: Generating emails from templates server-side

I need to send emails from MeteorJS application and I want to generate them using html templates, not by "html-in-js" stuff.
What I've tried to do:
1) Use Template.emailTemplate(data), but Template is not defined server-side.
2) Save my email templates as *.html files under <app>/server/email/templates directory, get their contents using fs.readSync() and then compile/render it using meteor's built-in handlebars package.
This works fine in development environment, but fails in production using bundled app because of *.html files under server directory are not bundled. Besides, the structure of directories is changed during bundle process and relative paths to templates become invalid.
3) Your proposals? =)

like image 593
th0r Avatar asked May 13 '13 20:05

th0r


3 Answers

Currently, templates are not supported server-side. That functionality is coming. In the mean time, I created a package you might find useful called handlebars-server that allows you to use Handlebars on the server. You can use the package with atmosphere or by copying the project directory into your packages folder. Here is an example:

Example:

my-email.handlebars

Hello, {{name}}

server.js

Email.send({
  html: Handlebars.templates['my-email']({ name: 'Chris' })
});

Note

No templates in the handlebars file. Just put your html and Handlebars expressions. The file will get compiled into a function and assigned to a property on the Handlebars.templates object. The property name will be the name of the file minus the handlebars extension.

Github

https://github.com/eventedmind/meteor-handlebars-server

like image 73
cmather Avatar answered Oct 19 '22 00:10

cmather


Another option now is to use the server side 'private' directory to read resources out of and use them to store resources your application will use.

create the meteor project, and then create a /private directory.

Place your templates in there (You should use the meteor-handlebars-server package instead if you need handlebars)

Read in your template with:

Assets.getText(assetPath, [asyncCallback]);

Obviously you can also do pattern matching regex/replace against the string once it's loaded.

example:

var template = Assets.getText(assetPath);  // Synchronous
var username = 'John Doe';

template = template.replace('{{username}}', username);
Email.send({
  html: template
});

For more info on the assets functionality: Meteor Assets

like image 23
MrMowgli Avatar answered Oct 19 '22 00:10

MrMowgli


Meteor 0.8.*, here is another solution.

https://gist.github.com/fpoirier1/534bf5db69ece2c83205

like image 1
Frank6 Avatar answered Oct 19 '22 00:10

Frank6