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.66k stars 339 forks source link

How to set template engine helpers #398

Closed andrew-lp closed 4 years ago

andrew-lp commented 4 years ago

Hi,

I am unable to find any recent documentation on how to add a custom helper to the handlebar instance used by email-templates

Is this possible on the latest versions?

Thanks

niftylettuce commented 4 years ago

This issue should be filed in the consolidate repo

EricHype commented 3 years ago

Sorry to necro a closed issue, but it might be helpful for others wandering here to see a working example without having to comb through consolidate's source as I did.

The answer is to pass helpers inside of the locals object, like so:

  const emailHtml = await email.render(
    `${pathToTemplate}/html`,
    {
      ...templateVariables,
      helpers: {
        isdefined(key) {
          return key !== undefined;
        }
      }
    }
  )