On Jun 18, 2009, at 10:03 PM, djolley wrote:

>
>> Not sure what you mean by conditionally... if it's something server
>> side you could just do:
>>
>> <% if this_expression_evaluates_to_true %>
>>   put your html here
>> <% end %>
>
> That is exactly what I'm talking about and it gives me a great basis
> for trying to articulate what is confusing me.
>
> I am apparently under the mistaken impression that when we insert into
> an HTML document Ruby code that is included within <% %> like this:
>
> <% ruby code %>
>
> the inserted Ruby code is run at the point where it is inserted into
> the HTML document.  When that Ruby code completes, any HTML code that
> follows is sent to the requesting browser.  The point is that at any
> given time either Ruby code is being run or HTML is being sent to the
> requesting browser.  What your Ruby code does is interact with a
> segment of HTML code so that that segment of HTML code is sent to the
> browser conditionally.  That just blows my mind.  How are we able to
> accomplish that?  Or, is that just something that I should accept and
> try not to think about too much?

Being inquisitive about the code you are running is a good thing. If  
you really want to know, every Ruby installation ships with erb.rb in  
the /path/to/lib/ruby/1.x/ directory. But the shorter path to your  
answer is that ERB scans your code for lines with <% and %> in them.  
The code inside is evaluated using the eval method. If there is an  
equal sign (<%=), then the results of the expression evaluation are  
concatenated to the end of the output stream. Anything outside the <%  
delimiters is treated as a string to be concatenated directly to the  
end of the output stream. This is an oversimplification, but that  
should give you an idea what makes things tick.

The key concept is that no output is generated and no expressions  
evaluated until the moment a page is rendered. The Ruby doesn't so  
much interact with the HTML code as to accept the HTML code as  
arbitrary strings that are (according to normal Ruby control flow  
statements) concatenated onto an output stream.

> I would think that if we were going
> to include content conditionally, that content would have to be within
> the <% %> and quoted.  My concern is that I am missing some very
> important point about <% %> that, if I weren't missing it, my
> understanding would be a whole lot better.
>
>> The way content_for works is to capture the result of the block
>> (everything in b/n) and save it for later.
>
> Yes.  I understand what content_for does.  I was just using it to
> provide an example of the type of thing that I was talking about.
>
> Thanks to all for the input.
>
>         ... doug
> >


--~--~---------~--~----~------------~-------~--~----~
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