Blank lines in conditional templates

4,885 views
Skip to first unread message

Arthur Clune

unread,
Apr 6, 2011, 1:22:19 PM4/6/11
to Puppet Users

I'm writing ldirectord.cf files via puppet and unfortunately
ldirectord is sensitive to blank lines in the file. I have the
following code in my .erb file

virtual=<%= vip %>:<%= port %>
<% if weights.empty? then -%><% real_servers.each do |real_server| -%>
real=<%= real_server %>:<%= port %> gate
<% end -%><% else -%><% real_servers.zip(weights).each do |
real_server, weight| -%>
real=<%= real_server %>:<%= port %> gate <%= weight %>
<% end -%><% end -%>
<% if not checktype.empty? then -%> checktype=<%= checktype -%><%
end -%>
<% if not service.empty? then -%> service=<%= service %><% end -
%>
<% if not checkcommand.empty? then -%> checkcommand=<%=
checkcommand %><% end -%>
protocol=<%= protocol %>
scheduler=<%= scheduler %>
persistent=<%= persistent %>

This all works fine. If (say) checktype == 'fred' then I get a line

checktype=fred

My problem is that is checktype == '', the checktype = line isnt'
printed (correct) but I get a blank line instead.

e.g.

virtual=192.168.1.1:53
real=192.168.1.2:53 gate
real=192.168.1.3:53 gate


protocol=udp
scheduler=rr
persistent=300


How can I stop those blank lines from appearing?

Thanks

Arthur

Ben Hughes

unread,
Apr 7, 2011, 12:26:11 AM4/7/11
to puppet...@googlegroups.com
On Wed, Apr 06, 2011 at 10:22:19AM -0700, Arthur Clune wrote:

>
> I'm writing ldirectord.cf files via puppet and unfortunately
> ldirectord is sensitive to blank lines in the file. I have the
> following code in my .erb file

I tried to recreate this, but your mailer has munged all the lines to fit
in 80 columns, so I can't tell where all the breaks should be, so can't
test it. Could you pastie or similar it?

--
Ben Hughes || http://www.puppetlabs.com/

Arthur Clune

unread,
Apr 7, 2011, 4:27:26 AM4/7/11
to puppet...@googlegroups.com
On Thu, Apr 7, 2011 at 5:26 AM, Ben Hughes <b...@puppetlabs.com> wrote:

> I tried to recreate this, but your mailer has munged all the lines to fit
> in 80 columns, so I can't tell where all the breaks should be, so can't
> test it. Could you pastie or similar it?

Thanks for looking at this. I've put it up at gist

https://gist.github.com/907330

I've tried various variations on the line layout but all seem to give
the same result: if the name isn't defined, I get a blank line rather
than no line at all.

Arthur


--
Arthur Clune art...@clune.org

Arthur Clune

unread,
Apr 7, 2011, 5:55:14 AM4/7/11
to puppet...@googlegroups.com
It's been suggested that I just create the output string in code,
which works fine. I've put a working version in the gist.

> https://gist.github.com/907330

--
Arthur Clune art...@clune.org

jcbollinger

unread,
Apr 7, 2011, 11:36:27 AM4/7/11
to Puppet Users
I haven't done much with ERB, but I used to work a lot with JSP, to
which ERB seems very similar. In what follows, I take the somewhat
risky approach of projecting JSP semantics onto ERB, so do take it
with several grains of salt. I'm sure those more knowledgable about
ERB itself will jump in where I err.

1. It is important to understand what the ERB processor is going to do
with your template, which is basically to turn it inside out to yield
a piece of executable Ruby code. Everything inside the ERB tags goes
into the output as Ruby code (decorated, in the case of <%= %>), and
everything outside gets turned into string literals and Ruby code to
output them.

2. "Everything outside" the ERB tags includes literal newlines in your
template, so normally those are echoed when the template is processed,
subject to the Ruby control flow. This, I believe, is the basis for
the comments I see in some ERB tutorials to the effect that "the
standard <% %> tag pair will result in a newline being added to the
output." I think that's incorrect. Reading the docs of the ERB
class, I think it's actually a <% %> pair *followed by a newline* that
results in a newline being added to the output -- and then only
because the newline is present in the template.

3. ERB has a "trim mode" option, which in ERB dialects such as Rails'
and Puppet's is enabled on a per-tag basis via the closing tag "-%>".
Some describe trim mode simplistically as suppressing "the added
newline", but ERB doesn't add a newline in the first place! The ERB
docs themselves say something subtly different: "omit newline for
lines ending in %>". In trim mode, then, ERB *modifies your template
text* by removing certain newlines.

4. So the $64,000 question is this: which newlines will be removed
from your template text? In particular, why weren't newlines removed
where you expected? I strongly suspect that a close, literal reading
of the ERB docs yields the answer. The newline is suppressed for
lines *ending in* "%>", but not, one might interpret, for lines that
have anything between the closing tag and the terminating newline.
Say, for example, space characters. Check for trailing spaces in your
template.


Regards,

John

Arthur Clune

unread,
Apr 7, 2011, 2:04:44 PM4/7/11
to puppet...@googlegroups.com


Thank you! There were indeed spaces at the end of lines.

Arthur

--
Arthur Clune

Reply all
Reply to author
Forward
0 new messages