Is there any way to make a EEx template trim all whitespace?
I’ve seen that at some point, in 1.4.16, this ability that was there for all templates was removed and only kept for HTML templates.
The issue now is that I can’t find a way to keep the old behavior for a non HTML template (markdown in my case), and my markdown generated files have a lot of extra \ns, and markdown is a bit sensitive when placing extra \n in some places.
I couldn’t find any simple workaround that doesn’t involve changing a lot the way I generate the output with templates.
I would like if there was a way to indicate that certain templates want trimming enabled but that seems like a feature request.
That’s my question, how to make markdown correct.
In particular this is what’s happening:
<%= if (false), do: "header" %>
<%= if (false), do: "body" %>
<%= if (true), do: "footer" %>
This outputs: \n\nfooter, but before 1.4.16 the \ns were trimmed on empty lines and the output was just footer.
How can I not output those extra \n on empty lines?
Now, it doesn’t look like you can configure Phoenix.Template.EExEngine to set that option, so what I’d do is create your own MyApp.EExEngine that does, and configure Phoenix to use that instead.
ps. I don’t understand the meaning of that phrase at all - it seems to be saying that some newlines are retained while some are not, without specifying when newlines are retained and when not.
This could be a solution if I was using EEx directly, but I’m leveraging on Phoenix templates so all my non html templates are precompiled already with trim: false at the moment of rendering. And even if I could configure my template engine, the trimming option is one level above the engine and it’s set to false for all non html. I could solve it rewording my template a bit using the format @kokolegorille suggested. Thanks!
Maybe like this?
<%=
if (false), do: “header”
%><%=
if (false), do: “body”
%><%=
if (true), do: “footer”
%>
This worked! I had to do some bigger changes because my real case was more complicated than this example but it did the trick, thanks!
This is one case where more (lines) is less (output)