const ejs = require('ejs');
const path = require('path');

function renderPage(routePath, res, options = {}, fragment = false) {
  ejs.renderFile(path.join(process.cwd(), 'views', routePath), options, (err, str) => {
    if (err) {
      return res.status(500).send(err.message);
    }

    if (fragment) {
      res.send(str);
    } else {
      res.render('layout', { body: str, ...options });
    }
  });
}

module.exports = renderPage;
