mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.74k stars 843 forks source link

Functions do not return embedded HTML #742

Open pautasso opened 1 year ago

pautasso commented 1 year ago

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.

RyanZim commented 1 year ago

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.

pautasso commented 1 year ago

(...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.