Senior & Expert RoR Developers Discussion Forum by Nyros Technologies

HIRE Ruby on Rails Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

Difference between %> and -%> tags.

 
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Experts Support & Chit Chat
View previous topic :: View next topic  
Author Message
swaminadhan



Joined: 14 Sep 2009
Posts: 84

PostPosted: Wed Feb 17, 2010 7:15 am    Post subject: Difference between %> and -%> tags. Reply with quote

Hi friends !

In ruby on rails , the normal content can be passed directly without any changes.However the content between <%= %> is interpreted as the ruby code and gets executed. The result of that execution is converted into string and that value is substituted into the file in place of the <%=...%>.


In editors you can also see something like <% %>(without an equal sign).
The code in between the <% %>(without an equal sign) in .rhtmls is interpreted as a Ruby code that is executed with no substitution back into the output.Interestingly this kind of process can also mixed with non-ruby code.


For example :


<% 3.times do %>
Hello !
<% end %>
Friend !


The above code will generate the following output.


Hello

Hello

Hello

Friend!

Note how the text in the file within the Ruby loop is sent to the output ,
once for each iteration of the loop.


But there will a strange thing is that we will get the blank lines in between each line.They cam from the input file.

If you think about it, the original file contains an end-of-line character (or characters) immediately after the %> of
both the first and third lines of the file. So, the <% 3.times do %> is stripped out of the file, but the newline remains. Each time around the loop, this newline is added to the output file, along with the full text of the Hello line. This accounts for the blank line before each Hello! line in the output. Similarly, the newline after <% end %> accounts for the blank line between the last Hello and the Friend! line.

Normally it doesn't matter a lot in html, because html doesn't much about the white space.


If you want to remove these blank lines,do this by changing the end of the ERB sequence from %> to -%>(with minus sign) .The minus sign tells rails to remove any new line that follows from the output. If we add a minus in the example

<% 3.times d0 -%>
Hello
<% end -%>
Friend!

We will get the output as follows:

Hello
hello
hello
Friend!

Adding a minus on the line containing end, gets rid of the blank line before Friend!.


Thank you,
swaminadhan
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Experts Support & Chit Chat
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts How to customize Google Maps InfoWindow using ym4r_gm
0 kalyanallampalli 49 Thu Sep 02, 2010 5:34 pm
kalyanallampalli View latest post
No new posts Drag and Drop file upload
0 umamageshwari 45 Wed Sep 01, 2010 9:36 am
umamageshwari View latest post
No new posts Flex On Rails
0 Raghu 45 Wed Sep 01, 2010 3:00 am
Raghu View latest post
No new posts MongoDB
0 Raghu 44 Tue Aug 31, 2010 3:30 pm
Raghu View latest post
No new posts Problem with Dreamweaver / java error
3 Raja 36 Tue Aug 31, 2010 3:24 pm
Raghu View latest post
No new posts Generating Model
0 phani.galla 43 Tue Aug 31, 2010 2:09 pm
phani.galla View latest post
No new posts Hide Panels using JQuery
0 Anu 46 Tue Aug 31, 2010 1:50 pm
Anu View latest post
No new posts Load a text file using ajax in jquery
0 Anu 35 Tue Aug 31, 2010 1:39 pm
Anu View latest post
No new posts Generating Migrations
0 phani.galla 29 Tue Aug 31, 2010 1:38 pm
phani.galla View latest post
No new posts Generating Controllers
0 phani.galla 31 Tue Aug 31, 2010 1:22 pm
phani.galla View latest post




Hire an expert Ruby on Rails developer / coder / programmer or development team from India now!!

Other Forums : PHP   ::   .Net   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About Ruby on Rails Experts   |   More