eta-dev / eta

Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
https://eta.js.org
MIT License
1.41k stars 65 forks source link

Compile "includes" in the templates #252

Closed sviridoff closed 1 year ago

sviridoff commented 1 year ago

Hi, it would be great to have a compile function that not only compiles the code but also compiles the includes within it. This would be useful for a webpack loader, for instance.

instead something like that:

__eta.res+='<!DOCTYPE html>\n<html lang="en">\n  <body>\n    Hi '
__eta.res+=__eta.e(it.name)
__eta.res+='!\n    '
__eta.res+=include("./tpls/menu.eta")
__eta.res+='  </body>\n</html>\n'

to a:

__eta.res+='<!DOCTYPE html>\n<html lang="en">\n  <body>\n    Hi '
__eta.res+=__eta.e(it.name)
__eta.res+='!\n    '
__eta.res+=include((__eta) => {
__eta.res+='This is a menu template';
})
__eta.res+='  </body>\n</html>\n'

I would appreciate hearing your opinion regarding the proposal

nebrelbug commented 1 year ago

@sviridoff, I think this is a great idea for a plugin or otherwise as an extension of the Eta template engine!

This would break the ability of templates to reference themselves conditionally, e.g.

partials/comment.eta

<% if (it.childComment) { %>
<%~ include("/partials/comment.eta") %>
<% } %>

So I won't change the builtin behavior.