You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use ejs for both server and client side rendering.
But I found out that the clientside functions (eventhough templates can be compiled with async: true) are not really able to handle asynchronous 'include' functions.
So there are a few options to modify the behavior the 'include' function to make it situation-fit.
On the server side there is ejs.fileLoader which allows for the actual file-reading to be modified. This won't work because on the client-side there is no access to any fileSystem (used prior to the ejs.fileLoader.
There is a includer property in ejs.Options, but it is poorly documented and not clearly referenced in the ejs.js file. So I am not sure if it is usable (I assume only server-side as the passed arguments are already refactured paths).
There is a third argument for the ejs.AsyncClientFunction and ejs.ClientFunction which can be used client-side for polyfilling the include function.
Only the third option is useable on the clientside as far as I gathered, but its function does not allow asynchronous actions which can be really bad for performance and usability reasons.
In my case I load a subset of templates from the server and use them clientside.
I would be able to do so syncronously with an XMLHttpRequest with the async flag set to false, but that obviously would be a verry bad idea. I am pretty sure this approuch wouldn't be usable when working with Workers either.
In any case there would be a huge benefit to be able to use asyncrhonous functions. I am not sure how/where it is implemented, otherwise I would have tried to create a PR, But the following snippet would allow both sync and async functions to be used as includer.
// suppose something like this would be used to obtain an `include` statementletincludedString=includer(path,data);// easily refactured to allow both sync and async versions of `includer`, assuming the context is async as well!letincludedString=awaitPromise.resolve(includer(path.data));
Would love to hear your feedback on this, or any suggestions on how to temp-fix this issue in the meantime!
The text was updated successfully, but these errors were encountered:
Hello everyone,
I use ejs for both server and client side rendering.
But I found out that the clientside functions (eventhough templates can be compiled with
async: true
) are not really able to handle asynchronous 'include' functions.So there are a few options to modify the behavior the 'include' function to make it situation-fit.
On the server side there is
ejs.fileLoader
which allows for the actual file-reading to be modified. This won't work because on the client-side there is no access to any fileSystem (used prior to theejs.fileLoader
.There is a
includer
property inejs.Options
, but it is poorly documented and not clearly referenced in the ejs.js file. So I am not sure if it is usable (I assume only server-side as the passed arguments are already refactured paths).There is a third argument for the
ejs.AsyncClientFunction
andejs.ClientFunction
which can be used client-side for polyfilling theinclude
function.Only the third option is useable on the clientside as far as I gathered, but its function does not allow asynchronous actions which can be really bad for performance and usability reasons.
In my case I load a subset of templates from the server and use them clientside.
I would be able to do so syncronously with an
XMLHttpRequest
with theasync
flag set tofalse
, but that obviously would be a verry bad idea. I am pretty sure this approuch wouldn't be usable when working with Workers either.In any case there would be a huge benefit to be able to use asyncrhonous functions. I am not sure how/where it is implemented, otherwise I would have tried to create a PR, But the following snippet would allow both sync and async functions to be used as
includer
.Would love to hear your feedback on this, or any suggestions on how to temp-fix this issue in the meantime!
The text was updated successfully, but these errors were encountered: