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

The following:

<% _erbout += Time.now %>

and

<% concat Time.now, binding %>

are the same as:

<%= Time.now %>

Greg H. wrote:

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

While this is the way to do it, I would suggest writing multi-line ruby
code as a helper and to return the HTML-Code. Something like this:


module YourControllerHelper
def my_output(model)
name = strip_tags(model.name)
description = strip_tags(model.description)
html = <<HTML

#{name}


#{description}


HTML
end
end

View:

<%= my_output %>

Another version is to substitute the here_doc with a render-call using a
partial.
There are multiple versions to accomplish this, but the above is the
quickest to write ;).

Remco Van 't veer wrote:

The following:

<% _erbout += Time.now %>

and

<% concat Time.now, binding %>

are the same as:

<%= Time.now %>

Greg H. wrote:

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

<% @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

On May 19, 9:24 pm, “Greg H.” [email protected]

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

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 D. with Rails” book
in
this context which is interesting.

Greg H. wrote the following on 20.05.2007 00:41 :

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