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

How to use ejs "include" in templates #324

Closed BenjaminDish closed 5 years ago

BenjaminDish commented 6 years ago

Hello,

I am trying to use this project with ejs. I have a lot of pain to understand how the rendering system works and how the template files coulds / should be structured and chained.

In my case, I can't maket it work without having one folder by template, containing a html.ejs file. Is it normal and mandatory or can we specify a filePath ?

Here is my app structure :

app.js
templates
   - emails
     - administrators
       - bottom-wrapper
         - html.ejs
       - notification-note
         - html.ejs
       - top-wrapper
         - html.ejs

I want to render the notification-note template, which contains :

<%- include('../top-wrapper') %>

TEST

<%- include('../bottom-wrapper') %>

Here is the way I'm calling it :

const email = new Email({
     message: message,
     transport: EmailService.transport(),
     views: {
          options: {
              extension: 'ejs'
          },
          root: path.join('templates', 'emails')
     }
 });
return email.send({
      template: "administrators/notification-note",
      locals: locals
})

I run into this error :

Could not find the include file "../top-wrapper"

I can't figure out how to make this work. Did someone already do this ?

niftylettuce commented 6 years ago

I think this is related https://github.com/niftylettuce/email-templates/issues/298#issuecomment-385241419 - maybe you have to have full path to the file from root of the project? That doesn't make sense - seems like we have a bug somehow either in consolidate or in email-templates. Maybe you can help discover it for us all 👍? You can also run with DEBUG=* node app.js flag or DEBUG=email-templates node app.js for more debugging output. Also see https://github.com/niftylettuce/email-templates/issues/225#issuecomment-300814108 for another approach that was suggested probably due to this same bug.

BenjaminDish commented 6 years ago

Hello, actually I guess it is more due to ejs. I tried doing the exact same thing using pug and pug "extends" worked perfectly using a relative path to the rendered template file.

Maybe we met a limit of what ejs can do ?

Concerning debugging, I would be glad to help but I don't even know what we should consider as a bug because I don't know the expected behavior. The docs doesn't specify if naming the templates "feature-folder/html.{ext}" is mandatory or could be changed. I also don't know if it is supposed to be possible to refer to "../" folder in the ejs docs.

Nevertheless, I chose to run with pug finally which seems to be simpler, more powerful and actually works

johnfrades commented 5 years ago

Hi, i have the same situation and here's how i solve it

   app.js
   - emails
     - new-question
       - html.ejs
       - subject.ejs
     - views
       - header.ejs
       - footer.ejs

and on the html.ejs of new-question

<% include ../views/header.ejs %>

CONTENT

<% include ../views/footer.ejs %>
gunjarinagaraju commented 5 years ago

Is there any way to send (pdf, xlsx ) files through the ejs templates. send through mails

c831216 commented 4 years ago

<%- include('file path'); -%>

It worked for me when I added the '-' at the end

domadev812 commented 2 years ago

@c831216 I am using ejs3.x and email-template module doesn't render include, it only render the content.

<head>
    <%- include('../partials/head') -%>
  </head>

This is my ejs template and I get [object Promise] instead of rendered text.