Hi everyone, coming from PHP something in the .rhtml files has been confusing the heck out of me. Why does this work? <% for header in @all_headers %> <%= header.subject %> <% end %> and both of these don''t work <% for header in @all_headers puts header.subject end %> --------------------------------------- <%= for header in @all_headers puts header.subject end %> Is there a way to output a ruby variable to the browser without using the <%= %> tags (and only using the <% %>)? Why can''t I use a puts/print/p <variable> to output to the browser? Am I missing something? Thanks, JD
Jean-Daniel Beaubien wrote:> Hi everyone, coming from PHP something in the .rhtml files has been > confusing the heck out of me. > > Why does this work? > <% for header in @all_headers %> > <%= header.subject %> > <% end %> > > and both of these don''t work > <% for header in @all_headers > puts header.subject > end %> > --------------------------------------- > <%= for header in @all_headers > puts header.subject > end %> > > > Is there a way to output a ruby variable to the browser without using > the <%= %> tags (and only using the <% %>)? Why can''t I use a > puts/print/p <variable> to output to the browser? > > Am I missing something? > > Thanks, > > JDHi JD, <% is for server side execution (logic), <%= is for something you want rendered to the client. Therefore, <%= header.subject %> is used to output the subject of the header to the browser. If you put <% header.subject %> then you wouldn''t see anything in the browser. Hope that helps. Michael -- Posted via http://www.ruby-forum.com/.
Jean-Daniel Beaubien wrote:> Hi everyone, coming from PHP something in the .rhtml files has been > confusing the heck out of me. > > Why does this work? > <% for header in @all_headers %> > <%= header.subject %> > <% end %> > > and both of these don''t work > <% for header in @all_headers > puts header.subject > end %> > --------------------------------------- > <%= for header in @all_headers > puts header.subject > end %> > > > Is there a way to output a ruby variable to the browser without using > the <%= %> tags (and only using the <% %>)? Why can''t I use a > puts/print/p <variable> to output to the browser? >The real question is - why would you use puts/print etc? That would be doubling things up - the view code in Rails handles the output for you - you just type what you want (within <%= %> tags if you need logic). puts makes the view look ... too "programmish" to me. If you have to use logic the way you suggest, i suggest you look up render_inline or render :inline, which makes it possible to do these kinds of calculations in the controller. Regards, Henning Kilset
Jena-Daniel - because .rhtml uses ERb as a templating language, not an inline scripting language. So it behaves more like Smarty - and Smarty encourages you not to use print statements, either. In a templating language, you don''t want to be using print statements - you want to be substituting the display of variables for where content should be in the flat template. puts/print dumps to STDOUT, iirc. You don''t want to dump to STDOUT. You want to include a variable in a template to be rendered. <%= %> means exactly that - display this variable in the page you render. You should be getting used to <%= %> syntax, rather than trying to work around it. Approaching pages as proper templates, rather than taking the PHP one-big-file mentality to it is a practical and good idea - I say this as a full-time client-side developer. Hope that clarifies some things, JD. t. On 23/07/06, Jean-Daniel Beaubien <jd@jackdan.net> wrote:> Hi everyone, coming from PHP something in the .rhtml files has been > confusing the heck out of me. > > Why does this work? > <% for header in @all_headers %> > <%= header.subject %> > <% end %> > > and both of these don''t work > <% for header in @all_headers > puts header.subject > end %> > --------------------------------------- > <%= for header in @all_headers > puts header.subject > end %> > > > Is there a way to output a ruby variable to the browser without using > the <%= %> tags (and only using the <% %>)? Why can''t I use a > puts/print/p <variable> to output to the browser? > > Am I missing something? > > Thanks, > > JD > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Just for reference, you would need to use _erbout.concat (header.subject) instead of put. But don''t do this. Use <%= %> . http://railsexpress.de/blog/articles/2005/12/06/trim-your-output - dan -- Dan Kohn <mailto:dan@dankohn.com> <http://www.dankohn.com/> <tel:+1-415-233-1000> On Jul 24, 2006, at 2:19 AM, Tom Armitage wrote:> Jena-Daniel - > > because .rhtml uses ERb as a templating language, not an inline > scripting language. > > So it behaves more like Smarty - and Smarty encourages you not to use > print statements, either. > > In a templating language, you don''t want to be using print statements > - you want to be substituting the display of variables for where > content should be in the flat template. > > puts/print dumps to STDOUT, iirc. You don''t want to dump to STDOUT. > You want to include a variable in a template to be rendered. <%= %> > means exactly that - display this variable in the page you render. > > You should be getting used to <%= %> syntax, rather than trying to > work around it. Approaching pages as proper templates, rather than > taking the PHP one-big-file mentality to it is a practical and good > idea - I say this as a full-time client-side developer. > > Hope that clarifies some things, JD. > > t. > > On 23/07/06, Jean-Daniel Beaubien <jd@jackdan.net> wrote: >> Hi everyone, coming from PHP something in the .rhtml files has been >> confusing the heck out of me. >> >> Why does this work? >> <% for header in @all_headers %> >> <%= header.subject %> >> <% end %> >> >> and both of these don''t work >> <% for header in @all_headers >> puts header.subject >> end %> >> --------------------------------------- >> <%= for header in @all_headers >> puts header.subject >> end %> >> >> >> Is there a way to output a ruby variable to the browser without using >> the <%= %> tags (and only using the <% %>)? Why can''t I use a >> puts/print/p <variable> to output to the browser? >> >> Am I missing something? >> >> Thanks, >> >> JD >> >> >> >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails