On Apr 7, 3:27 am, Arthur Clune <art...@clune.org> wrote:
> 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.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

Reply via email to