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? =)
我需要从MeteorJS应用程序发送电子邮件,我想使用html模板生成它们,而不是通过“html-in-js”的东西生成它们。我试图做的事情:1)使用Template.emailTemplate(数据),但模板未定义服务器端。 2)将我的电子邮件模板保存为
3 个解决方案
#1
14
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:
目前,服务器端不支持模板。这个功能即将到来。与此同时,我创建了一个名为handlebars-server的软件包,它允许您在服务器上使用Handlebars。您可以使用包含大气的包或将项目目录复制到packages文件夹中。这是一个例子:
Example:
例:
my-email.handlebars
我-email.handlebars
Hello, {{name}}
server.js
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.
手柄文件中没有模板。只需输入你的html和Handlebars表达式。该文件将被编译为函数并分配给Handlebars.templates对象上的属性。属性名称将是文件的名称减去手柄扩展名。
Github
Github上
https://github.com/eventedmind/meteor-handlebars-server
https://github.com/eventedmind/meteor-handlebars-server
#2
4
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.
创建一个meteor项目,然后创建一个/ private目录。
Place your templates in there (You should use the meteor-handlebars-server package instead if you need handlebars)
将模板放在那里(如果你需要把手,你应该使用meteor-handlebars-server包)
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
有关资产功能的更多信息:Meteor Assets
#3
1
Meteor 0.8.*, here is another solution.
流星0.8。*,这是另一个解决方案。
https://gist.github.com/fpoirier1/534bf5db69ece2c83205
https://gist.github.com/fpoirier1/534bf5db69ece2c83205
#1
14
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:
目前,服务器端不支持模板。这个功能即将到来。与此同时,我创建了一个名为handlebars-server的软件包,它允许您在服务器上使用Handlebars。您可以使用包含大气的包或将项目目录复制到packages文件夹中。这是一个例子:
Example:
例:
my-email.handlebars
我-email.handlebars
Hello, {{name}}
server.js
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.
手柄文件中没有模板。只需输入你的html和Handlebars表达式。该文件将被编译为函数并分配给Handlebars.templates对象上的属性。属性名称将是文件的名称减去手柄扩展名。
Github
Github上
https://github.com/eventedmind/meteor-handlebars-server
https://github.com/eventedmind/meteor-handlebars-server
#2
4
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.
创建一个meteor项目,然后创建一个/ private目录。
Place your templates in there (You should use the meteor-handlebars-server package instead if you need handlebars)
将模板放在那里(如果你需要把手,你应该使用meteor-handlebars-server包)
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
有关资产功能的更多信息:Meteor Assets
#3
1
Meteor 0.8.*, here is another solution.
流星0.8。*,这是另一个解决方案。
https://gist.github.com/fpoirier1/534bf5db69ece2c83205
https://gist.github.com/fpoirier1/534bf5db69ece2c83205