Doug,

I recall I went through a similar 'wow' experience, and it helped me
sort out how this was working when I realised I could  think of <% and
%> in the reverse of the way they are normally presented.

By that I mean, insted of seeing a <% %> pair as bracketing ruby code
embedded in the middle of HTML, think of it as a %> <% pair bracketing
text in the middle of ruby code. (This needs a bit of mind-twisting at
first - not to mention odd details like an assumed %> at the beginning
of the document.) Then imagine the resulting patterns such as

    %>html text<%

as replaced by ruby code such as

   puts "html text"

and it is easy to see why the text is output conditionally when it
sits inside an 'if' statement.

(I am not suggesting that erb (or anything else) actually works this
way, but I found it helped me see what was going on. YMMV ;-)  )

Having read subsequent posts, I would comment that I have also
discovered the joys of HAML and would not now use anything else.

Graeme

On Jun 20, 7:28 pm, djolley <[email protected]> wrote:
> > "_erbout = ''
> > _erbout.concat \"This is plain text\\n\"\n_erbout.concat \"And the
> > time is \"
> > _erbout.concat(( Time.now ).to_s)
> > _erbout.concat \"\\n\"\n
> > if true
> >    _erbout.concat \"\\n\"\n_erbout.concat \"  it was true\\n\"\n
> > else
> >    _erbout.concat \"\\n\"\n_erbout.concat \"  it wasn't true\\n\"\n
> > end
> > _erbout.concat \"\\n\"\n_erbout"
>
> Thanks so much, Steve.  I think that I get the essence of how it
> works.  It does not seem that erb directly translates an input
> document into an output stream.  Rather, it appears that erb produces
> a Ruby program that, when run, produces the output stream.  Lines of
> the input document that consist wholly of text (i.e., they do not
> contain any '<%' beginning markers or '%>' ending markers) are passed
> as an argument to a method called erbout.concat which adds the line to
> the output stream when the program is run.  Thus, the intermediate
> program contains a series of calls to erbout.concat (one for each text
> line).  Thus, if the input document contains no lines which include
> beginning or ending markers, the intermediate program consists
> exclusively of a series of calls to the erbout.concat method (one for
> each line of input) with the relevant line passed as an argument to
> the method.  If such a program is run, it simply outputs the original
> document.
>
> Now, let's see what happens when <% %> and <%= >%> markers are
> introduced into the document.  First of all, any such content is
> treated as being placed on a separate line.  So, if we we had:
>
> The quick brown <% fox %> jumped over the lazy dog.
>
> That would be treated as being:
>
> The quick brown
> <% fox %>
> jumped over the lazy dog
>
> The 'The quick brown' and 'jumped over the lazy dog' are now just
> plain lines of text and as such they are treated accordingly by each
> being passed as an argument to a call to the erbout.concat method.
> What do we do with <% fox %>?  Pretty much nothing.  We through away
> the beginning and ending markers and leave simply 'fox'.  'fox' is now
> a fragment of Ruby code which will be run along with the other lines
> of code, e.g., calls to erbout.concat when the program is run.
>
> That only leaves us with figuring out how content within <%= and %>
> markers is treated.  That's pretty easy.  The enclosed content is
> evaluated as a Ruby statement and the output is treated as a line of
> text which is passed as an argument to a separate call to
> erbout.concat.
>
> I think that's basically it.  I feel much better now.  I welcome any
> comments on my analysis.
>
> Thanks to all who contributed.
>
>           ... doug
>
> P.S. Colin, I didn't mean to ignore you; but, I didn't really see
> anything in the Getting Started Guide that would help me with this
> issue.  Thanks for the input though.
>
> jumped over the lazy dog

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to