Haoqi Haoqi wrote:
> demo.html.erb
> <% 3.times do%>
> <%=rand%>
> <%end%>
> it has 6 '%'!
> how to make less '%' like this?
> <%
>   3.times do
>     puts rand
>   end
> %>

Option 1:

<%=
  res=""
  3.times do
    res << "#{rand}\n"
  end
  res
%>

(and you can make a helper method to abstract out this pattern)

Option 2:

<%
  3.times do
    _erbout << "#{rand}\n"
  end
%>

This is more fragile, as the name of the _erbout variable can be changed 
at runtime, and may not be compatible with other ERB implementations 
(e.g. erubis)
without specific configuration. See
http://www.kuwata-lab.com/erubis/users-guide.03.html#erbout-enhancer

Option 3:
Use erubis with PrintEnabledEnhancer. See
http://www.kuwata-lab.com/erubis/users-guide.03.html#printenabled-enhancer
I don't know if this enables "puts" as well as "print", and it's an 
erubis-specific function.

Option 4:
Ditch ERB entirely and switch to HAML. Your example becomes just:

- 3.times do
  = rand

You'll either love HAML or hate it. Personally I love it. My templates 
are a fraction of the size they were before, and I never have to worry 
about unbalanced XHTML tags or missing 'end' on a block.

HTH,

Brian.
-- 
Posted via http://www.ruby-forum.com/.