mcmath / ejs-html-loader

Webpack loader for rendering HTML from EJS templates
MIT License
47 stars 9 forks source link

Is it possible to pass locals to the template "on the fly"? #6

Closed jpsear closed 7 years ago

jpsear commented 7 years ago

I'm aware that I can pass local variables into all ejs files using my loader configuration, but is it possible to do this when consuming each template file?

Without a loader (when not bundling), I was doing this:

const renderEmail = (filePath, data) => {
  return ejs.renderFile(filePath, data, (error, result) => {
    if (error) {
      console.log(error)
      return
    }
    return result
  })
}

let html = renderEmail('path-to-template.ejs', { randomParams: 'abc' })

Essentially, I want my locals to be able to change on a per-template basis...

Sorry if it's a slightly silly question!

jpsear commented 7 years ago

Gah, ignore me. The above seems to work just fine!

mcmath commented 7 years ago

Actually, you've reminded me that this loader needs to be updated for Webpack 2.

But as to your question, you should be able to include the loader multiple times with different locals:

rules: {
  { test: 'path/to/one.ejs', use: {
    loader: 'ejs-html',
    options: { randomParams: 'abc' }
  } },
  { test: 'path/to/two.ejs', use: {
    loader: 'ejs-html',
    options: { randomParams: '123' }
  } }
}

If this doesn't work, it may just be that it needs updating, which I will do shortly. I'll be sure to include a test case for this scenario.