npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

templatey

v0.1.1

Published

A Simple Email Template Engine

Downloads

3

Readme

Templatey - A Simple Email Template System for NodeJS

Template is a simple email template system that allows you to write templates in handlebars just like you would write templates for your websites, then you just pass the appropriate data into the template for email generation.

Installation

npm install templatey

Usage

You can have a directory holding the templates written with handlebars ending in .hbs. What this means is that you can share the template directories for your express app with templatey.

You can also use ad hoc templates. Below shows how to use templates both ways.

Initialize the Template Service

var service = Template.make(options);

The option objects passed in will instantiate the appropriate service. Templatey uses NodeMailer for sending emails, so any combination of appropriate parameters expected by NodeMailer will work here.

Examples of some NodeMailer parameters are

{
  "type": "SMTP",
  "service": "Gmail",
  "auth": {
    "user": <gmail_user_name>,
    "pass": <gmail_password>
  }
}

Besides the NodeMailer options, the following are additional options expected by templatey`.

  • rootPath - points to the root folder of your predefined hbs templates

  • args - default arguments for the emails you would send, for example:

    { "from": <the_default_email_address>, "subject": <the_default_subject> ... }

Sending Emails

To send an email, use the service object that you have instantiated.

service.send(args, data, function(err) { ... });

The args are the default settings for your email, and the data` are what would be used in template merging.

Args are similar to the options.args passed into templatey.make, with any email parameters that you would want to have as default.

Here are some examples that you can use (basically the same as NodeMailer)

{
  "from": <from_email_address>
  , "to": <to_email_address>
  , "subject": <subject_of_the_email>
  ...
}

Using Predefined Templates

To use predefined template, have a template parameter to be a relative path of the specific pre-defined template you are instantiating.

For example, let's say we have the the following folder structure for templates:

/
  /msg
     forgetPasswordResult.hbs
     forgetUserResult.hbs
  layout.hbs

To refer to forgetUserResult.hbs, pass in

{
  ...
  "template": "msg/forgetUserResult"
  ...
}

Using Ad Hoc Templates

To pass in an ad hoc template, just use the template parameter to hold the particular template, and then simply add a type parameter to specify the type of the template.

{
  ...
  "template": "This is a markdown message. Hello, {{ user.name }}.",
  "type": "markdown"
  ...
}

Currently, templatey handles markdown and html for the templates.

The Data Parameter

The data object is basically any data that your template would expect, plus a couple of parameters that'll be merged into the args object.

{
  "email": <for_replacing_to_email_address>
  "subject": <if specified will replace subject in the arg>
  ... anything else your templates expect...
}

If you pass in an array - it'll be iterated through to create multiple emails.

Express Middleware

You can use templatey with express.

var templatey = require('templatey');

app.use(templatey.middleware(options)); // option is the same as the description above.

The middleware will subscribe to a sendMail event raised through res object, so to send email, you just do the following in your route.

function(req, res) {
  res.emit('sendMail', {args: <the_args_param>, data: <the_data_param});
}