Hi all what is the difference between <%= and <% in a view file? thanks -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
<%= prints the contents of the tag, whereas <% does not -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/DImbzNgRNCYJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
And you couldnt just try that out yourself? :D On Aug 18, 7:39 pm, Pepe Sanchez <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi all > > what is the difference between <%= and <% in a view file? > > thanks > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tim Shaffer wrote in post #1017353:> <%= prints the contents of the tag,Well, it never meant that. <%= tells ruby to print the result of the expression between the tags. For instance, <%= 2+2 %> would not print ''2 + 2'', it would print ''4''. But even the "result of the expression" isn''t an entirely accurate description--because in rails 3 you write: <%= form_for(@user) do |f| %> which doesn''t fit that description. In ruby, ''form_for(@user) do |f|'' isn''t an expression--it''s the first half of a loop/block, so it''s an incomplete expression. In other words, in ruby if you wrote: result = some_func(arg) do |x| you would get an error. Maybe its best to think of <%= form_for() as an exception to the rule? One example of how <%= and <% differ is this: <% 3.times do |i| %> <div>loop: <%= i %></div> <% end %> The <% tags just execute the ruby code and don''t enter any text onto the page, which causes ruby/.erb to loop over the <div> tag 3 times. The <%= tag inside the loop prints the value of the variable i. So you get: <div>loop: 0</div> <div>loop: 1</div> <div>loop: 2</div> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 18, 2011, at 6:05 PM, 7stud -- wrote:> Tim Shaffer wrote in post #1017353: >> <%= prints the contents of the tag, > > Well, it never meant that. <%= tells ruby to print the result of the > expression between the tags. For instance, > > <%= 2+2 %> > > would not print ''2 + 2'', it would print ''4''. But even the "result of > the expression" isn''t an entirely accurate description--because in > rails > 3 you write: > > <%= form_for(@user) do |f| %> > > which doesn''t fit that description. In ruby, ''form_for(@user) do | > f|'' > isn''t an expression--it''s the first half of a loop/block, so it''s an > incomplete expression. In other words, in ruby if you wrote: > > result = some_func(arg) do |x| > > you would get an error. Maybe its best to think of <%= form_for() > as an > exception to the rule? > > One example of how <%= and <% differ is this: > > <% 3.times do |i| %> > <div>loop: <%= i %></div> > <% end %> > > The <% tags just execute the ruby code and don''t enter any text onto > the > page, which causes ruby/.erb to loop over the <div> tag 3 times. The > <%= tag inside the loop prints the value of the variable i. So you > get: > > <div>loop: 0</div> > <div>loop: 1</div> > <div>loop: 2</div> >Here''s another way to look at this. ''<%='' is a shortcut for ''<% puts''. There''s a similar construction in PHP: ''<?='' is a shortcut for ''<?php echo (or print)''. Walter> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > . >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you all for your answers! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Davis wrote in post #1017505:> Here''s another way to look at this. ''<%='' is a shortcut for ''<% puts''. > There''s a similar construction in PHP: ''<?='' is a shortcut for ''<?php > echo (or print)''. > > WalterI do not believe you can use ''<% puts'' in ERB, i thought you are supposed to use ''<% concat'' Alexey. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 21, 2011, at 3:34 AM, Alexey Muranov wrote:> Walter Davis wrote in post #1017505: > >> Here''s another way to look at this. ''<%='' is a shortcut for ''<% >> puts''. >> There''s a similar construction in PHP: ''<?='' is a shortcut for ''<?php >> echo (or print)''. >> >> Walter > > I do not believe you can use ''<% puts'' in ERB, i thought you are > supposed to use ''<% concat''Thanks! I never tried this, actually. Walter> > Alexey. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > . >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.