forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

Clearer documentation on how to pass template custom filters #327

Closed PatrickVanRietschoten closed 5 years ago

PatrickVanRietschoten commented 6 years ago

I just implemented this custom filter:

  const email = new Email({
    message: {
      from: "example@example.org",
    },
    transport,
    // uncomment below to send emails in development/test env:
    send: true,
    views: {
      root: path.join(__dirname, "..", "emails"),
      locals: {
        filters: {
          'my-own-filter': function (text, options) {
            if (options.addStart) text = 'Start\n' + text;
            if (options.addEnd) text = text + '\nEnd';
            return text;
          }
        },
      },
    },
  })
    :my-own-filter(addStart addEnd)
      Filter
      Body

Inspired by: https://pugjs.org/language/filters.html

However, it took me almost 2 hours to find out how. I eventually found out by looking at Lad's source code for translations.

Would be better to have this in the documentation I think...

The rest is pretty awesome! Keep it up!