2

I am working to generate offer letter for employees. So I have some default template index.html:

index.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<div id="header">
<h1>Offer Letter</h1>
</div>

<div id="name">
 Hello [[Name]],

Welcome to our organization,'

Details:

[[Date]]    : Offer Letter Date
[[Name]]    : Employee Name
[[Designation]] : Designation
[[Address]] : Communication Address
[[CTC]]  : Cost to the Company

</div>

<div id="footer">
Copyright
</div>

</body>
</html>

I am generate multiple offers at a time more than 300.

So I have json data like this:

var obj = [
 { "name" : "Abc",
  "Date" : "12-12-2014",
  "Designation" : "Admin",
  "Address" : "XXXX",
  "CTC" : 3333},
{ "name" : "wefrwe",
  "Date" : "12-12-2014",
  "Designation" : "Admin",
  "Address" : "XXXX",
  "CTC" : 55},
{ "name" : "dssd",
  "Date" : "12-12-2015",
  "Designation" : "Admin",
  "Address" : "XXXX",
  "CTC" : 333},
{ "name" : "gfsc",
  "Date" : "12-1-2014",
  "Designation" : "Admin",
  "Address" : "XXXX",
  "CTC" : 22},
{ "name" : "dssdds",
  "Date" : "1-12-2014",
  "Designation" : "Admin",
  "Address" : "XXXX",
  "CTC" : 4334}
];

How do I replace values (eg. [[Name]] to Abc ) .

Have any node packages ?

I tried the following code but don't know this is best way and useful.

for(i in obj) {


var textbody = '<!DOCTYPE html> <html> <head> </head> <body> <div id="header"> <h1>Offer Letter</h1> </div> <div id="name"> Hello [[name]], Welcome to our organization,' Details: [[Date]] : Offer Letter Date [[Name]]    : Employee Name [[Designation]] : Designation [[Address]]   : Communication Address [[CTC]]  : Cost to the Company </div> <div id="footer"> Copyright </div> </body> </html>';


textbody = textbody.replace(/{[^{{}}]+}/g, function(key){
    return obj[key.replace(/[{{}}]+/g, "")] || "";
});

}
3
  • What you're looking for is a template engine. Commented Feb 6, 2016 at 6:37
  • The main concept is. We have html content(index.html) in tables. So I need to render data form database table and read then replace the content then need to create html file and pdf file. Commented Feb 6, 2016 at 6:41
  • Well, the template engine will help generate your HTML dynamically using the data from your database. You can use a tool like PhantomJS to create a PDF from a webpage (see: phantomjs.org/api/webpage/method/render.html). Commented Feb 6, 2016 at 6:48

2 Answers 2

3

mustache.js is a zero-dependency implementation of the mustache template system in JavaScript.

Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object.

Sign up to request clarification or add additional context in comments.

Comments

0

Refer this for sending email using node, nodemailer & jade

How to send an html page as email in nodejs

Your jade template should be similar(or better than) to:

doctype html
html
    head
    body
        p
            | Hello #{newemp.name},
            br
            br
            | Welcome to our organization.
            br
            br
            | Details:
            br
            | #{newemp.Date}  : Offer Letter Date
            br
            | #{newemp.name}  : Employee Name
            br
            | #{newemp.Designation}  : Designation
            br
            | #{newemp.Address}  : Communication Address
            br
            | #{newemp.CTC}  : Cost to the Company

and

in the referred link, you need to pass the context as below:

var context = { 
                 "name" : "Abc",
                 "Date" : "12-12-2014",
                 "Designation" : "Admin",
                 "Address" : "XXXX",
                 "CTC" : 3333
              };

I did harcode the context variable with one employee object for simplicity, you need to dynamically pass each employee object for each email.

1 Comment

jade, pug, similar things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.