ejs
ejs copied to clipboard
include not a function error
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
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.
Using EJS 3.1.3 with Express 4.17.1 and cannot get includes to work either include not a function
without async.
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 spent 3 hours looking for the error beacuse i was using 'client' . thanks alot
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/
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"