I have a view partial that is used to render a small block of HTML
content. Because of where it's going to be placed in the HTML, it's
important that it not begin or end with any HTML whitespace.
But making a sample view partial that contains nothing but a simple
string, just to test things out -- when rendered it seems to still end
with a newline, which is of course HTML whitespace. Even though there's
no newline in the html.erb file.
Is there any simple way to make a partial view render with no newline at
the end -- or the more general question, what I'm really after, with no
HTML whitespace at the start or end of the partial view content?
My (wild) guess is that it's actually your ERB tag that's adding the
newline.
You can use a minus sign as part of the closing ERB tag to indicate
that you don't want the newline:
<%= render :partial => 'blah' -%>
Aha, good point! I think you're right.
In general, for very white-space-dependent ERB partials, I'm finding it
tricky to get the white space right. Are there any special tricks people
have in general for controlling whitespace while still leaving readable
views, other than <%- and -%>. I know about those. But even with those,
I'm finding I have to jam html tags together in order to get the
whitespace I want.
Hmm, I just thought of one. Instead of an actual literal:
<div>
content
</div>
in the html, which leaves no good way to control output whitespace
without also making the HTML kind of hard to read, I could use:
<%- tag("div") do -%>
<%- "content" -%>
<%- end -%>
Which lets me exercize complete control of whitespace supression without
actually having to eliminate it in my source. Does that make any sense,
or is it completely ridiculous? It's still kind of hard to read, but in
deeply nested html, still not as hard to read as "<div>lots of
stuff<div>more stuff<span>more stuff</span>stuff</div></div>" all jammed
together without whitespace, because I don't want any whitespace in the
output.