Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions do not return embedded HTML #742

Open
pautasso opened this issue Aug 31, 2023 · 2 comments
Open

Functions do not return embedded HTML #742

pautasso opened this issue Aug 31, 2023 · 2 comments

Comments

@pautasso
Copy link

pautasso commented Aug 31, 2023

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.

@pautasso pautasso changed the title Function do not return embedded HTML Functions do not return embedded HTML Aug 31, 2023
@RyanZim
Copy link
Collaborator

RyanZim commented Aug 31, 2023

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
Copy link
Author

pautasso commented Sep 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants