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

Is it possible to set handlebars partials? #346

Closed theprobugmaker closed 5 years ago

theprobugmaker commented 5 years ago

I was wondering if it's possible to set handlebars partials? I feel like I'm repeating myself a lot in the email templates, something that I could easily get rid of using partials by just including it.

Thank you

theprobugmaker commented 5 years ago

@niftylettuce Why is this closed?

binarytracer commented 5 years ago

@zefexdeveloper i also what to know if thats possible.

basically i manage to "customized" the creation of content on this line: https://github.com/niftylettuce/email-templates/blob/master/src/index.js#L152

ive override the the function, what i mean is that from there maybe we can use all handlebars can do.

Ive never tried working on these idea yet.

niftylettuce commented 5 years ago

Specify the partials option in your configuration.

const Email = require('email-templates');

const email = new Email({
  message: {
    from: 'niftylettuce@gmail.com'
  },
  transport: {
    jsonTransport: true
  },
  views: {
    options: {
      extension: 'hbs',
      partials: {
        myPartial: 'partials/myPartial'
      }
    }
  }
});

Source: https://github.com/tj/consolidate.js/blob/master/lib/consolidate.js#L872-L874

Ref: https://github.com/tj/consolidate.js/issues/252

niftylettuce commented 5 years ago

If this works please submit PR to this repo's README with an example. Thank you.

binarytracer commented 5 years ago

seems that approach is not working anymore,

i tried this definition

new EmailTemplate({
  htmlToText: false,
  views: {
    root: path.resolve('emailing/views'),
    options: {
      extension: 'hbs',
      partials: {
        samplePartial: '---just-a-simple-text----',
      }
  },
  },
});

i got this error

UnhandledPromiseRejectionWarning: Error: The partial samplePartial could not be found
    at Object.invokePartial (/home/meegle/tools/node_modules/handlebars/dist/cjs/handlebars/runtime.js:281:11)
    at Object.invokePartialWrapper [as invokePartial] (/home/meegle/tools/node_modules/handlebars/dist/cjs/handlebars/runtime.js:68:39)
    at Object.eval [as main] (eval at createFunctionContext (/home/meegle/tools/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:257:23), <anonymous>:8:28)

it seems it cant detect the partial being registered, im following this example here

https://handlebarsjs.com/partials.html


Round 2 I found another way to inject the partials, as i read some insights here: https://documentup.com/niftylettuce/node-email-templates

Example:

// override the engine first
handlebars.registerPartial('samplePartial', 'this is partial')

module.exports = new EmailTemplate({
  htmlToText: false,
  views: {
    root: path.resolve('emailing/views'),
    options: {
      extension: 'hbs',
  },
  },
});

and this works for me

LitvinenkoEvgeny commented 3 years ago

Yeah after debugging i see that partials doesn't pass to handlebars engine at all "email-templates": "^8.0.2", image