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 to output to the browser?
Am I missing something?
Thanks,
JD
Jean-Daniel B. 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 to output to the browser?
Am I missing something?
Thanks,
JD
Hi 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
Jean-Daniel B. wrote:
puts header.subject
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 K.
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.
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 K. mailto:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000