On Jun 19, 2009, at 11:51 AM, djolley wrote:
capability in question in an example; but, it really isn’t discussed.
block or am just super dense. Thanks again.
Ok, I oversimplified it. This discussion really belongs on the ruby-
talk ML, but here is the longer explanation. Assuming you have the code:
This is plain text
And the time is <%= Time.now %>
<% if true %>
it was true
<% else %>
it wasn’t true
<% end %>
Then after compiling it, ERB creates:
"_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 ;
Without getting into the details of how ERB scans and tokenizes the
text, the point I want to make is that what you thought was HTML is
nothing more than a bunch of strings to ERB. These strings are passed
to eval, which evaluates and runs them as Ruby code. So check it out.
I just reformatted the code above:
“_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_erbout.concat " it was true\n”\n
_erbout.concat “\n”\n_erbout.concat " it wasn’t true\n”\n
And you can see how the if and else affect what strings are output.
For a more in-depth example, just do what I did: Create the ERB, punch
it into rdebug, and step through it. Ain’t it cool to have source?