开发者

Ruby on Rails HTML template logic

I'm brand new to Ruby and Rails.

I've been asked to help modify a RoR site with a lot of legacy code, where the original developer is gone. When I look at the erb HTML templates, I see code like this in several places:

<% if( @fullscreen == 1 ) %>
<%= "<div class='full'><p>...</p></div>" %>
<% end %>

Is there a reason for the <%= and %> on the second line? It seems like it just prints the exact quoted string, so there would be no difference from putting the <div> code by itself on that line without the bracket-percent bookends. Everything seems fine when I take it out, 开发者_运维百科but I don't want to miss anything subtle.

Thanks.


The <%= %> tags mean to execute the enclosed Ruby code, and print out what it returns to the HTML.

Since in this case the Ruby code is just a string, there's no difference between

<%= "<div class='full'><p>...</p></div>" %>

and

<div class='full'><p>...</p></div>

other than the way in which it's executed. The latter is likely a bit faster since it doesn't need to execute any Ruby code.

Note that as bunter mentioned below, this is only true if the code omitted within the <p> tag doesn't contain any embedded Ruby code, e.g. <p>#{@my_variable}</p>.


No differences, as long as this ellipsis (...) does not contain any ruby code. Maybe your developer had some code in ther once. Pure html does not need erb tags.

T.


You don't miss anything. There's no difference :-)


I agree and will add just one additional piece of information - it would matter if you were rendering a javascript template like this:

format.js {render :layout => false}.

For example, here's code from a jscript template that emits code for Google Maps:

var student_latlng = new google.maps.LatLng("<%= student.geocode.latitude.to_s %>", "<%= student.geocode.longitude.to_s %>");

Hope this helps,

Russ

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜