0

I am reading my stylesheet file and storing it in a variable which I would then like to pass in to my EJS template as the styles for that page.

This is what I have in server.js file:

function getRenderVariables (req) {
  const MY_CSS = fs.readFileSync(path.resolve(__dirname, './css/my-css.css'), 'utf8')

  return {
    MY_CSS
  }
}

app.get('/mypage', (req, res) => {
  res.render('index', getRenderVariables(req))
})

For the record, the MY_CSS variable is appropriately reading the file and it's value as its being passed in, is what I expected. In mypage.ejs I would like to use the styles from the loaded stylesheet. How do I do that?

<div class="some-class">
  <style>
    // Not working! How can I do this?
    <%= MY_CSS %>
  </style>

  <div>
    Other stuff....
  </div>
</div>
theJuls
  • 6,788
  • 14
  • 73
  • 160
  • Possibe duplicate with [enter link description here](https://stackoverflow.com/questions/18629327/adding-css-file-to-ejs?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – gary_byrne20 Apr 20 '18 at 19:41
  • It may be a duplicate, but the one response that was relevant to me (by John Papa) didn't work. The styles either simply do not load or, even worse the content of the css file is actually displayed. – theJuls Apr 20 '18 at 19:56

0 Answers0