javascript - Meteor templates not recognized in server code -


i building app in meteor , trying send email server-side method. email's template exists in client/views folder. when try send email server-side code, error 'template not defined'. here's code:

/server/methods/sendemail.js

var html = blaze.tohtmlwithdata(template.emailtocustomer, datacontext); meteor.call('sendemail',                  thisuser.emails[0].address,                 'mine@mine.org',                 'yours@gmail.com',                  'email subject',                   html                 ); 

my email template simple html page. note code works fine if exists in client/views/mytemplate.js. however, technical reasons, need email sent server-side code.

i have tried placing mytemplate.html , mytemplate.js files in app/both , app/server folders, still template not defined error.

anyone have ideas might going on?

thanks!

your templates defined under client directory it's normal not available in server context.

unfortunately it's not possible @ moment access blaze templates using vanilla meteor apis @ moment, if move templates shared directory available both client , server.

what can use meteorhacks:ssr define server templates you'll able render using standard spacebars syntax.

you'll need put server templates under server private directory , compile them in server code using :

ssr.compiletemplate("emailtocustomer", assets.gettext("emailtocustomer.html")); 

server templates don't need <template> tag, can define them straight away :

private/emailtocustomer.html

<p>hello {{name}}</p> <p>this server-side rendering !</p> 

in server code, once template compiled, can define helpers in standard client-side blaze templating :

template.emailtocustomer.helpers({   name: function(){     return this.firstname + " " + this.lastname;   } }); 

then can render template using :

var html = ssr.render("emailtocustomer", {   firstname: "john",   lastname: "doe" }); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -