ejs icon indicating copy to clipboard operation
ejs copied to clipboard

include not a function error

Open richlewis14 opened this issue 4 years ago • 6 comments

Hi,

I am positive that the above error is caused by my implementation or use of ejs, but I have a scenario where I am trying to render a partial within a partial, this particular section of the view gets rendered after an Ajax call (I'm trying to split my views into smaller components)

I have a view file views/home.ejs and within that I have <%- include('test-component') %>

The error I get is include is not a function

When sending the rendered html back to the client it is compiled through

const readAndCompileFile = async filePath => {
  const file = await readFile(filePath);
  return ejs.compile(file, { client: true });
};

const sendHtml = async (data, res) => {
  const template = await readAndCompileFile(TEMPLATE_FILE_PATH);
  const html = template({ ...data, Utilities, moment });
  return res.send({ html });
};

I hope this is enough information but if anything further is needed then please advise

I am on the latest version of ejs, 3.1.3

Thank You

richlewis14 avatar Jun 12 '20 16:06 richlewis14

I would need to see more code to understand what you're trying to do, and what's not working. From your initial description, it's possible that the error stems from your asynchronous rendering steps, but this is just a guess.

mde avatar Jun 14 '20 17:06 mde

Using EJS 3.1.3 with Express 4.17.1 and cannot get includes to work either include not a function without async.

woodscreative avatar Jul 26 '20 15:07 woodscreative

const readAndCompileFile = async filePath => {
  const file = await readFile(filePath);
  return ejs.compile(file, { r_client: true });
};

If you using "include" in .ejs file, don't use client

eyupahmetyilmaz avatar Nov 25 '21 17:11 eyupahmetyilmaz

@eyupahmetyilmaz spent 3 hours looking for the error beacuse i was using 'client' . thanks alot

ahmedseada avatar Dec 09 '21 19:12 ahmedseada

My mistake here was that I didn't provide a proper callback for rendering of included template, but only on top-level template rendering, which resulted in exactly that error. A working (kinda minimal) example: https://jsfiddle.net/cjw57p6n/41/

LeoSko avatar Jun 12 '22 10:06 LeoSko

const readAndCompileFile = async filePath => {
  const file = await readFile(filePath);
  return ejs.compile(file, { r_client: true });
};

If you using "include" in .ejs file, don't use client

thank you very much. spend so many hours because this word "client"

UdayaBandara avatar Jul 26 '22 16:07 UdayaBandara