views:

165

answers:

3

Sometimes it's more convenient to print in <%%>. How to do it in Rails?

+1  A: 

erb has two method to evaluate inline ruby expressions. The <% which evaluates the expression and the <%= which evaluates and prints. There is no global object to print to within the binding context.

As mentioned by Omar, there is a concat method, which is part of ActionView. This will do what you want.

Unlike a scripting language escape, there is no default output for erb. Since erb is simply a function, and given a template and binding will return a variable, it returns the values of text and functions recursively.

There is hot debate as to how much logic should be allowed in a view, but as little as possible is what most people aim for. If you are putting more code than text in the view, you may want to consider refactoring your code.

brianegge
+3  A: 

In ERB: The <% %> signify that there is Ruby code here to be interpreted. The <%= %> says output the ruby code, ie display/print the result.

So it seems you need to use the extra = sign if you want to output in a standard ERB file.

Otherwise, you could look at alternatives to ERB which require less syntax,.. maybe try something like HAML. http://haml-lang.com/tutorial.html

Example:

ERB
<strong><%= item.title %></strong>

Haml
%strong= item.title

Is that more convenient?

Evolve
+3  A: 

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M001742

Should be what you are looking for

Omar Qureshi