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.35k stars 60 forks source link

Control token for *preserving* whitespace #264

Open osyvokon opened 8 months ago

osyvokon commented 8 months ago

Problem The default for whitespace control is to trim newlines after the tag. This is sensible in most cases. However, sometimes I'd like to preserve the newline. For example:

One
<%= num2str(1 + 1) %>
Three

This is expected to be rendered to

One
Two
Three

but the default output is

One
Two Three

Describe the solution you'd like Introduce a tag that preserves whitespace overruling global settings. Something like |%>. The example above would become this:

One
<%= num2str(1 + 1) |%>
Three

Describe alternatives you've considered

  1. Put an extra new line after the tag, but this doesn't look good:

    One
    <%= num2str(1 + 1) %>
    
    Three
  2. Set autoTrim: false, but that requires trimming whitespace manually (by using <%- and -%>). Again, for the most part I'm happy with autoTrim, but in some rare cases need to override it.

Let me know if you think this is a sensible feature. If so, I can open a pull request.

Thanks!

nebrelbug commented 8 months ago

This is an intriguing idea! I'll ask people what they think in the Eta Discord.