Functions do not return embedded HTML
pautasso opened this issue · 2 comments
view.ejs file:
<% function f() { %>
<p>from function</p>
<% } %>
<%-include("include", {f}) %>
include.ejs file:
from include
<main><%-f() %></main>
The output will be:
<p>from function</p>
from include
<main></main>
As opposed to:
from include
<main><p>from function</p></main>
Functions emit the embedded html by appending it to the output of the view in which they are declared. If they are called from another view, they will not use the "correct" output buffer of the .ejs in which they are called from but the one in which they have been declared.
Yeah, the function just appends at call time. This can be demonstrated by the fact that <% f() %>
(scriptlet tag, not an output tag) will still output <p>from function</p>
. If you want your function to actually return something, you'd write it like this:
<% function f() {
return '<p>from function</p>'
} %>
Of course, you wouldn't be able to use EJS inside the function. Defining functions in EJS is generally inconvenient; you probably want to make your function an included partial instead.
(...leading to many small partial include files, which could have been functions instead)
Thanks for the explanation, maybe the documentation could be clarified to say something like:
One can use functions with ejs, but those functions do not return the embedded HTML/JS content.