I can''t seem to find this in the Agile rails book or the online docs... I want to be able to kick out small snippets of text without making new code blocks, so instead of: <% for item in @items product = item.product -%> <div><%= item.quantity %></div> <% end -%> i''d rather say something to the effect of: <% for item in @items product = item.product puts "<div>#{item.quantity}</div>" end -%> Obviously this is a contrived example, but I think you all know what I mean. tia -- Brock Weaver [OBC]Technique
Straight from the Rails wiki (http://api.rubyonrails.com/classes/ActionView/Base.html) You trigger ERb by using embeddings such as <% %> and <%= %>. The difference is whether you want output or not. Consider the following loop for names: <b>Names of all the people</b> <% for person in @people %> Name: <%= person.name %><br/> <% end %> The loop is setup in regular embedding tags (<% %>) and the name is written using the output embedding tag (<%= %>). Note that this is not just a usage suggestion. Regular output functions like print or puts won’t work with ERb templates. So this would be wrong: Hi, Mr. <% puts "Frodo" %> (If you absolutely must write from within a function, you can use the TextHelper#concat) Brock Weaver wrote:>I can''t seem to find this in the Agile rails book or the online docs... > >I want to be able to kick out small snippets of text without making >new code blocks, so instead of: > ><% for item in @items > product = item.product >-%> ><div><%= item.quantity %></div> ><% end -%> > >i''d rather say something to the effect of: > ><% for item in @items > product = item.product > puts "<div>#{item.quantity}</div>" >end -%> > >Obviously this is a contrived example, but I think you all know what I mean. > >tia > > >
Brock Weaver
2005-Aug-22 18:12 UTC
Re: Equivalent of PHP''s echo or ASP''s response.write?
Didn''t know about concat -- exactly what I was looking for. Thank you. On 8/22/05, Jake Good <jake-UDG8+2NMCBIv34k0vaUUyg@public.gmane.org> wrote:> Straight from the Rails wiki > (http://api.rubyonrails.com/classes/ActionView/Base.html) > > You trigger ERb by using embeddings such as <% %> and <%= %>. The > difference is whether you want output or not. Consider the following > loop for names: > > <b>Names of all the people</b> > <% for person in @people %> > Name: <%= person.name %><br/> > <% end %> > > The loop is setup in regular embedding tags (<% %>) and the name is > written using the output embedding tag (<%= %>). Note that this is not > just a usage suggestion. Regular output functions like print or puts > won''t work with ERb templates. So this would be wrong: > > Hi, Mr. <% puts "Frodo" %> > > (If you absolutely must write from within a function, you can use the > TextHelper#concat) > > > > Brock Weaver wrote: > > >I can''t seem to find this in the Agile rails book or the online docs... > > > >I want to be able to kick out small snippets of text without making > >new code blocks, so instead of: > > > ><% for item in @items > > product = item.product > >-%> > ><div><%= item.quantity %></div> > ><% end -%> > > > >i''d rather say something to the effect of: > > > ><% for item in @items > > product = item.product > > puts "<div>#{item.quantity}</div>" > >end -%> > > > >Obviously this is a contrived example, but I think you all know what I mean. > > > >tia > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver [OBC]Technique
Nicholas Seckar
2005-Aug-23 06:22 UTC
Re: Equivalent of PHP''s echo or ASP''s response.write?
Brock Weaver wrote:> <% for item in @items > product = item.product > -%> > <div><%= item.quantity %></div> > <% end -%>This case is best handled by <%= @items.collect {|item| content_tag ''div'', item.quantity} %>
Zachery Hostens
2005-Aug-23 07:28 UTC
Re: Equivalent of PHP''s echo or ASP''s response.write?
<% items.each do |item| -%> <div><%= item %> <% end -%> id personally recommend this over trying to turn 50% of your templates into ruby itself. you just may kick yourself later on wanting to add an id or class to that div, or switching the design into a table for any given reason. On 8/23/05, Nicholas Seckar <nseckar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Brock Weaver wrote: > > <% for item in @items > > product = item.product > > -%> > > <div><%= item.quantity %></div> > > <% end -%> > > This case is best handled by > > <%= @items.collect {|item| content_tag ''div'', item.quantity} %> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Brock Weaver
2005-Aug-23 13:56 UTC
Re: Equivalent of PHP''s echo or ASP''s response.write?
Thank you everybody for all the advice. I will keep them in mind as I come across different situations. Have a good day! On 8/23/05, Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> <% items.each do |item| -%> > <div><%= item %> > <% end -%> > > id personally recommend this over trying to turn 50% of your templates > into ruby itself. you just may kick yourself later on wanting to add > an id or class to that div, or switching the design into a table for > any given reason. > > > On 8/23/05, Nicholas Seckar <nseckar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Brock Weaver wrote: > > > <% for item in @items > > > product = item.product > > > -%> > > > <div><%= item.quantity %></div> > > > <% end -%> > > > > This case is best handled by > > > > <%= @items.collect {|item| content_tag ''div'', item.quantity} %> > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver [OBC]Technique
George Hotelling
2005-Aug-23 18:49 UTC
Re: Equivalent of PHP''s echo or ASP''s response.write?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Aug 23, 2005, at 2:22 AM, Nicholas Seckar wrote:> Brock Weaver wrote: > >> <% for item in @items >> product = item.product >> -%> >> <div><%= item.quantity %></div> >> <% end -%> >> > > This case is best handled by > > <%= @items.collect {|item| content_tag ''div'', item.quantity} %>This wins, it''s elegant and a great example of the power of Ruby''s methods like Enumerable#collect and Rails'' helpers like content_tag. +1! George - -- George Hotelling GPG: 0x8175D485 ] http://george.hotelling.net ] _ _ _ ___ _ _ _/ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Darwin) iD8DBQFDC2/JgXVRXIF11IURAmGtAJ0SeG1uk+/uB3LuPxyUQ9q7YJjMMgCaAhqS mow+PLvu9vwPaEZGnmNXKS8=RxEW -----END PGP SIGNATURE-----
On 8/23/05, George Hotelling <george-s1Z0Dg4i37rMFIMGWPqnnw@public.gmane.org> wrote:> > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Aug 23, 2005, at 2:22 AM, Nicholas Seckar wrote: > > > Brock Weaver wrote: > > > >> <% for item in @items > >> product = item.product > >> -%> > >> <div><%= item.quantity %></div> > >> <% end -%> > >> > > > > This case is best handled by > > > > <%= @items.collect {|item| content_tag ''div'', item.quantity} %> > > This wins, it''s elegant and a great example of the power of Ruby''s > methods like Enumerable#collect and Rails'' helpers like content_tag. > +1!However, this is not readable as <% for item in @items product = item.product %> <div><%= item.quantity %></div> <% end -%> Using more abstract functions like that will increases the learning curve for others. In a collaborative development environment, I would prefer less helpers and more HTML code, especially in this case, where typing content_tag ''div'' is not a lot less typing than <div></div> _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Dave Silvester
2005-Aug-24 09:21 UTC
Re: Equivalent of PHP''s echo or ASP''s response.write?
David Zhao wrote:> However, this is not readable as > > <% for item in @items > product = item.product > %> > <div><%= item.quantity %></div> > <% end -%>Just a slight off-topic code-fascist tip here (aimed at the original poster), from the HTML perspective. Div tags have no real meaning - they are simply containers. Chances are there''s a more meaningful HTML tag you should use. I hope you occasionally test your stuff text only? If not, it''s a good habit to get into - either disable stylesheets completely (simple in Firefox using the Web Developer Toolbar), or use something like the Lynx or Links text-only browser. It will really open your eyes about the quality of your code. The reason I''m mentioning this is that I never directly put text in a div, only other items such as <h1>, <p> or <ul>, so that''s what set the alarm bells ringing, since it sounds like you''re building some slightly meaningless markup. Aside from better accessibility, the added bonus to sites that have semantic meaning when viewed text-only is that search engines are also basically text-only. I''m not saying it helps a lot, but since you have no reason not to add meaning to the raw code of your page where possible, it can only be a good thing to seize the chance to do it. Otherwise, we might as well just succumb to a major case of "divitus", put everything in divs, and do away with all those other pesky tags. ;-) If you care about making good code, use <div> and <span> sparingly and with caution, and always question yourself about why you really need to use them. No offense intended, and apologies if I''m preaching to the converted. :-) ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
Brock Weaver
2005-Aug-24 14:05 UTC
Re: Equivalent of PHP''s echo or ASP''s response.write?
I agree with your thoughts on HTML -- there are too many bad practices in use today. The example I originally posted was a contrived one to express my problem as simply as possible. I am a bit on the flipside as far as <div> and <span> go. To me, <table> is the most abused tag out there -- I would guess that *most* sites use it in one form or another to provide their primary layout of every page. <div> and <span> are better suited for this (ala CSS). While they are not typically the best for raw text (like you said), they do offer the benefit of no default styling applied aside from display:block or display:inline. <ul>, <p>, <h1> etc all have a default styling which you must either accept or override. So you could argue it either way I suppose. But in the long run, if it is valid XHTML, displays fine in Firefox, and doesn''t look that bad in IE, that''s good enough for me. On 8/24/05, Dave Silvester <dave-AJqNGCqIqVQ7cdpDWioORw@public.gmane.org> wrote:> David Zhao wrote: > > However, this is not readable as > > > > <% for item in @items > > product = item.product > > %> > > <div><%= item.quantity %></div> > > <% end -%> > > Just a slight off-topic code-fascist tip here (aimed at the original poster), > from the HTML perspective. > > Div tags have no real meaning - they are simply containers. Chances are > there''s a more meaningful HTML tag you should use. > > I hope you occasionally test your stuff text only? If not, it''s a good habit > to get into - either disable stylesheets completely (simple in Firefox using > the Web Developer Toolbar), or use something like the Lynx or Links text-only > browser. It will really open your eyes about the quality of your code. > > The reason I''m mentioning this is that I never directly put text in a div, > only other items such as <h1>, <p> or <ul>, so that''s what set the alarm bells > ringing, since it sounds like you''re building some slightly meaningless markup. > > Aside from better accessibility, the added bonus to sites that have semantic > meaning when viewed text-only is that search engines are also basically > text-only. I''m not saying it helps a lot, but since you have no reason not to > add meaning to the raw code of your page where possible, it can only be a good > thing to seize the chance to do it. > > Otherwise, we might as well just succumb to a major case of "divitus", put > everything in divs, and do away with all those other pesky tags. ;-) > > If you care about making good code, use <div> and <span> sparingly and with > caution, and always question yourself about why you really need to use them. > > No offense intended, and apologies if I''m preaching to the converted. :-) > > ~Dave > > -- > > Dave Silvester > Rent-A-Monkey Website Development > Web: http://www.rentamonkey.com/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver [OBC]Technique
Jarkko Laine
2005-Aug-24 15:17 UTC
Re: Equivalent of PHP''s echo or ASP''s response.write?
Brock, On 24.8.2005, at 17.05, Brock Weaver wrote:> I agree with your thoughts on HTML -- there are too many bad practices > in use today. The example I originally posted was a contrived one to > express my problem as simply as possible. > > I am a bit on the flipside as far as <div> and <span> go. To me, > <table> is the most abused tag out there -- I would guess that *most* > sites use it in one form or another to provide their primary layout of > every page. <div> and <span> are better suited for this (ala CSS). > > While they are not typically the best for raw text (like you said), > they do offer the benefit of no default styling applied aside from > display:block or display:inline. <ul>, <p>, <h1> etc all have a > default styling which you must either accept or override.But that is exactly the problem with divs and spans: they don''t mean anything. Of course you have to override the default styling in most cases but you have to do some styling with divs, too. If the default styling is your concern, consider trying Tantek Celik''s undo html css script [1] that pretty much resets all default formatting. Anyway, avoiding default formatting is a _bad reason_ for abandoning semantic and meaningful markup. Don''t get me wrong: div is an essential element in laying out standards-compliant websites. However, it should never be used instead of more meaningful elements like lists or headings where they are due. //jarkko [1] http://www.meyerweb.com/eric/thoughts/2004/09/15/emreallyem- undoing-htmlcss/ -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails