how to output HTML in a view within a <% %> (not a <%= %> )

Hi,

How can I output/render text within a view, but within a block of ruby code that is surrounded by <% %> ?

I know that use of <%= %> works, but if I have several lines of ruby in the view that is within <% %> and I want to output text to be part of the HTML how can I do this?

For example something like “puts” or “render” but actually works within a *.rthml view.

Thanks
Greg

<% @foo.each do |bar| %>

<%= bar %>

<% end %>

tks Ian - so it sounds like what I’m asking isn’t possible? (i.e. and you do have to break the script area back to HTML, if you know what I mean)

what exactly are you trying to do? Give me an example.

Remco gave you the answer: use the concat method.

Stylistically, though, you should only use the concat if it's truly
unavoidable. In general, you shouldn't be putting too much Ruby code
into your views. As Florian suggests, think about pushing this out to
a helper so that you can use the clean, readable <%= %> tags.

Chris

oh…I didn’t see a response from Remco yet(?)

I’m basically just curious to understand whether it was possible to output from within <% %>, to relate possible use within rails views with Java JSP. The answer seems to be that it is not possible, which is fine, I just wanted to understand.

So you have to do:
<% @foo.each do |bar| %>

<%= bar %>
<% end %>

As there is no way of doing something like:
<%
@foo.each do |bar|
render_to_view bar
end
%>

tks
Greg

Greg Hauptmann wrote the following on 20.05.2007 00:41 :

oh...I didn't see a response from Remco yet(?)

I'm basically just curious to understand whether it was possible to
output from within <% %>, to relate possible use within rails views
with Java JSP. The answer seems to be that it is not possible, which
is fine, I just wanted to understand.

So you have to do:
<% @foo.each do |bar| %>
  <%= bar %>
<% end %>

As there is no way of doing something like:
<%
   @foo.each do |bar|
     render_to_view bar
   end
%>

No, in fact the first code is roughly translated to the second version
using _erbout (or maybe concat). Nothing prevents you to directly using it :

<%
   @foo.each do |bar|
     _erbout += bar
   end
%>

ok thanks Lionel - I’ve understood the concat method too. Seems like concat would be the slightly less “under the bonnet” approach to use. Neither seemed to get mentioned in the “Agile Web Development with Rails” book in this context which is interesting.