Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print in ERB without <%=?

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

like image 640
Cheng Avatar asked Nov 15 '09 11:11

Cheng


People also ask

How do I print an ERB file?

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.

What is an ERB template?

An ERB template looks like a plain-text document interspersed with tags containing Ruby code. When evaluated, this tagged code can modify text in the template. Puppet passes data to templates via special objects and variables, which you can use in the tagged Ruby code to control the templates' output.

What is <% in Ruby?

Code in <% %> (without equal) is executed "with no substitution back into the output" means you want to execute code WITHOUT any output, like a loop and the best part is, it can be used with a non ruby code.


2 Answers

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-concat

Should be what you are looking for.

E.g. the following statement using concat:

<% concat "Output" %> 

is equivalent to:

<%= "Output" %> 
like image 136
Omar Qureshi Avatar answered Oct 17 '22 07:10

Omar Qureshi


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?

like image 29
Evolve Avatar answered Oct 17 '22 05:10

Evolve