<%= works, print doesn't


#1

In my views, for example list.rhtml, if I try:

<%= “test” %>

everything works fine. If I try:

<% print “test” %>

then instead of outputting the page, the browser displays the HTML
source code. Any ideas on what my problem is? This is my first RoR app
so it’s probably a stupid bug on my part, but my Google-fu isn’t
powerful to track down an answer.


#2

Try this.

Hi, Mr. <% puts “Frodo” %>

~ ryan ~


#3

Thanks for the quick reply! Using “puts” keeps the browser from
displaying HTML code, but it doesn’t output the text in the quotes.
I.e. the page displays properly, except the “puts” silently disappears.
Any other ideas?


#4

On Tue, 2005-12-27 at 23:29 +0100, Ben CH wrote:

so it’s probably a stupid bug on my part, but my Google-fu isn’t
powerful to track down an answer.

The problem with this (and the other example using puts) is that for
your rails app, standard output is the console (check it out if you’re
running Webrick for dev) - that’s where your output is ending up.

I’m not sure if there’s any way to get the output back via a command,
except perhaps <% return ‘foo’ %> - moral of the story is you probably
want to use <%=. If you’re building up a string to output, make that a
helper (a method in application_helper.rb) and have that method return
the correct output. Then <%= helper_method %>

  • Jamie

#5

On 12/27/05, Ben CH removed_email_address@domain.invalid wrote:

In my views, for example list.rhtml, if I try:

<%= “test” %>

everything works fine. If I try:

<% print “test” %>

This is by design. You are supposed to use <%= x %>. Using <% print
x %> will cause various problems depending on what STDOUT is. Same
thing with <% puts x %>. There is a Rails function named concat which
will do what you want. <% concat x %> is the same as <%= x %>.


#6

Jeremy E. wrote:

<% concat x %> is the same as <%= x %>.

Thanks for the response! I gave it a try, but couldn’t get it to work.
Doesn’t the concat operation take at least two parameters by definition?
I can see creating a string using the concat operator, then using <%=
concatedString %> to print it out, but I can’t think of any way that
simply doing <% concat x %> could work. Any clarification?


#7

On Dec 27, 2005, at 2:41 PM, Ben CH wrote:

removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

You cant use print or puts inside of erb tags like that. It will mess
up the output just like you have encountered. So this:

<%= @foo %> is the way to print @foo to the page. There is now
workaround for this , you must not use print or puts in the erb
templates period.

Cheers-

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#8

On Dec 27, 2005, at 3:50 PM, Ben CH wrote:

You cant use print or puts inside of erb tags like that. It will mess
up the output just like you have encountered. So this:

<%= @foo %> is the way to print @foo to the page. There is now
workaround for this , you must not use print or puts in the erb
templates period.

Now if you want to use the concat method you must do this:

<% _erbout.concat foo %> OR <% _erbout << foo %>

_erbout is the stream that your erb template is compiling itself
into. So concatting to that or appending with << will do what you are
after. But <%= is much less to type and I find that if you think you
need to use the _erbout var then something is wrong and you might
want to think about refactoring your code.

Cheers-

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#9

<% concat x %> is the same as <%= x %>.

Thanks for the response! I gave it a try, but couldn’t get it to work.
Doesn’t the concat operation take at least two parameters by definition?
I can see creating a string using the concat operator, then using <%=
concatedString %> to print it out, but I can’t think of any way that
simply doing <% concat x %> could work. Any clarification?

My mistake. You’d need to use <% concat x, binding %>. Note that the
api description states that “Using <%= concat “hello” %> would result
in a double hello.”, which is false, since it would probably result in
an error. I’m not sure why concat doesn’t use the binding of it’s
caller as a default if a binding is not specified explicitly,
presumably that’s either an oversight or a technical limitation of the
ruby interpreter.


#10

Jeremy E. wrote:

My mistake. You’d need to use <% concat x, binding %>.

Thanks for the clarification. I’m finding it hard to believe that there
is no simple way in Ruby to do <% print “true!” if something == true %>
on a webpage. Am I just a bad programmer for having massive amounts of
logic on my webpages that print things conditionally? I use the ternary
operator also, but that’s so limiting as well.


#11

What’s wrong with this:

<%= "true!" if something == true %>

#12

If you took the time and consulted the rails API documentation on the
RoR website you would have found this very useful bit…


concat(string, binding)

The regular puts and print are outlawed in eRuby. It’s recommended to
use the <%= “hello” %> form instead of print “hello”. If you
absolutely must use a method-based output, you can use concat. It’s
used like this: <% concat “hello”, binding %>. Notice that it doesn’t
have an equal sign in front. Using <%= concat “hello” %> would result
in a double hello.


#13

I don’t know how much code you need to have in your templates, but one
of
the traps you can fall into is having too much code in them. One of the
red
flags for this is needing large blocks of Ruby code.

RHTML files should be mostly HTML output, with a few places where you
drop
in your data.

If you do find you need to write a bunch of code to generate output,
then a
helper may be the better place for this code. If you have program logic
in
there, then that should be part of the controller.

One of the things I liked about the Velocity template language was that
it
was really hard to do more than just output. RHTML allows you to do a
bunch of cool Ruby things in the template file, but doing so is almost
always a bad idea.


#14

Tom F. wrote:

What’s wrong with this:

<%= “true!” if something == true %>

Okay, I used a poor example, but this issue has gone a little beyond my
earlier problem, i.e. simply printing out something without having to
devote an entire ruby tag to it. My problem is more along multiple
lines of code, e.g. a twenty line block of code with multiple print
statements in it. I hate having to break up a block of code just to
print something out.

What I wish:
<%
print “X”
print “Y”
%>

What I get:
<%= “X” %>
<%= “Y” %>

Doesn’t look so bad like that, but when you throw in a lot more logic
with the print statements sprinkled between them, having to break the
code into multiple <% %> blocks just to print something out is a little
frustrating.

Guess I’ll have to go back to PHP. :wink: J/k, RoR is too sweet for that at
the moment, but a guy IS working on PhpOnTrax (phpOnTrax.org), which is
basically a PHP port of RoR. (And it has a cooler acronym: POT) It’s
not entirely stable yet, but it’s getting there.