Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/outofsyncstudios/mail-template-helper

A simple library to merge data into email templates
https://github.com/outofsyncstudios/mail-template-helper

javascript node-js npm open-source

Last synced: about 1 month ago
JSON representation

A simple library to merge data into email templates

Awesome Lists containing this project

README

        

# mail-template-helper

[![NPM](https://nodei.co/npm/@outofsync/mail-template-helper.png?downloads=true)](https://nodei.co/npm/@outofsync/mail-template-helper/)

[![Actual version published on npm](http://img.shields.io/npm/v/@outofsync/mail-template-helper.svg)](https://www.npmjs.org/package/@outofsync/mail-template-helper)
[![Travis build status](https://travis-ci.org/OutOfSyncStudios/mail-template-helper.svg)](https://www.npmjs.org/package/@outofsync/mail-template-helper)
[![Total npm module downloads](http://img.shields.io/npm/dt/@outofsync/mail-template-helper.svg)](https://www.npmjs.org/package/@outofsync/mail-template-helper)
[![Package Quality](http://npm.packagequality.com/badge/@outofsync/mail-template-helper.png)](http://packagequality.com/#?package=@outofsync/mail-template-helper)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/43225424afb04627afd2e026712d5281)](https://www.codacy.com/app/OutOfSyncStudios/mail-template-helper?utm_source=github.com&utm_medium=referral&utm_content=OutOfSyncStudios/mail-template-helper&utm_campaign=Badge_Grade)
[![Codacy Coverage Badge](https://api.codacy.com/project/badge/Coverage/43225424afb04627afd2e026712d5281)](https://www.codacy.com/app/OutOfSyncStudios/mail-template-helper?utm_source=github.com&utm_medium=referral&utm_content=OutOfSyncStudios/mail-template-helper&utm_campaign=Badge_Coverage)
[![Dependencies badge](https://david-dm.org/OutOfSyncStudios/mail-template-helper/status.svg)](https://david-dm.org/OutOfSyncStudios/mail-template-helper?view=list)

`mail-template-helper` is a helper module to assist with compilation of email templates.

# [Installation](#installation)

```shell
npm install @outofsync/mail-template-helper
```

# [Usage](#usage)

```js
const MailTemplatesHelper = require('@outofsync/mail-template-helper');

const templates = {
['Test2']: {
subject: '{testData1} {testData2}',
template: '{bodyData} - {testData2} -- {embedded.value}'
}
};

const mailTemplateHelper = new MailTemplatesHelper(templates);

console.log(mailTemplateHelper.getFilledTemplate('Test2', {
testData1: 'a',
testData2: 'b',
bodyData: 'test',
embedded: {
value: 'qwerty'
}
}));
```

# [API Reference](#api)

## MailTemplatesHelper constructor(templates) ⟾ instanceof MailTemplatesHelper
Create an instance of MailTemplatesHelper with the `templates` collection provided.

## MailTemplatesHelper.getTemplate(templateName) ⟾ object(mail template) / null
Returns the template with the `templateName` provided or `null` if the template does not exist

## MailTemplatesHelper.getFilledSubject(templateName, data) ⟾ string / null
Returns the template subject from the `templateName` provided, using the `data` to fill any `{}` placeholders, or `null` if no template by that name exists.

## MailTemplatesHelper.getFilledBody(templateName, data) ⟾ string / null
Returns the template body from the `templateName` provided, using the `data` to fill any `{}` placeholders, or `null` if no template by that name exists.

## MailTemplatesHelper.getCustomFilled(template, data) ⟾ string / null
Returns custom `template` filled using the `data` to fill any `{}` placeholders, or `null` if the `template` is `null` or `undefined`

# [Templates and Collections](#templates)

## Template
A template is just a string with `{}` placeholders for data in data dictionary. The placeholders can reference nested data objects and array indices.

```js
const template = 'This is a template with { data }. Hello { name }.';
```

## Mail Template
A mail template is an object which contains two template strings labelled `body` and `subject`.

```js
const mailTemplate = {
subject: 'Mail Subject -- { data1 }',
body: 'This is a template with { data }. Hello { name }.'
};
```

## Template Collection
A template collection is an object with named indices which each contain a Mail Template.

```js
const templates = {
['Test1']: {
subject: 'Mail Subject -- { data1 }',
body: 'This is a template with { data }. Hello { name }.'
},
['Test2']: {
subject: 'Mail Subject -- { data1 }',
body: 'This is a template with { data }. Hello { name }.'
}
};
```

# [License](#license)

Copyright (c) 2018, 2019 Jay Reardon
Copyright (c) 2019 Out of Sync Studios LLC -- Licensed under the MIT license.