Forum: Ruby on Rails how to output something from within <% %> tags?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Filip G. (Guest)
on 2006-02-23 18:29
simple question: how do I output something from within <% %> tags? e.g.
like 'echo' in PHP. I thought it would be 'puts' or 'print' but neither
seems to work. right now I always close the %> and open a <%= which is
tedious.
Clint P. (Guest)
on 2006-02-23 18:39
(Received via mailing list)
You would use <% render :text => "whatever you want to write" %> but I'm
not
sure how that would be less tedious than <%= "whatever you want to
write" %>
Simo A. (Guest)
on 2006-02-23 18:48
Filip G. wrote:
> simple question: how do I output something from within <% %> tags? e.g.
> like 'echo' in PHP. I thought it would be 'puts' or 'print' but neither
> seems to work. right now I always close the %> and open a <%= which is
> tedious.


From API docs:
  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.


Simo
 Addsw.it
Filip G. (Guest)
on 2006-02-23 19:01
Clint P. wrote:
> You would use <% render :text => "whatever you want to write" %> but I'm
> not
> sure how that would be less tedious than <%= "whatever you want to
> write" %>

Thanx but that doesn't work; render :text only works from within the
controller and it won't render anything else anymore. I just need a
command to echo stuff within a view.

With an echo command I could do this:

<%
  code
  echo "whatever you want to write"
  more code
%>

Whereas now I have to do this:

<%
  code
%>
<%= "whatever you want to write" %>
<%
  more code
%>

And that is very tedious.

An echo command is so basic, surely RoR has this?
Filip G. (Guest)
on 2006-02-23 19:15
Simo Gal wrote:
> From API docs:
>   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.

Thanx, that works but the ", binding" part makes it bad. I tried to put
this in application_helper.rb

  def echo(str)
    concat(str, binding)
  end

but that generates an error

  undefined local variable or method `_erbout' for
#<#<Class:0x384b968>:0x384b548>


Sometimes RoR annoys the hell out of me. Why would they deliberately
'outlaw' a most basic function?
Erik van Oosten (Guest)
on 2006-02-23 19:40
(Received via mailing list)
It is simple. Where would puts output to?
My guess is it would go to stdout, so to the webrick console?

     Erik.


Filip G. schreef:
Ezra Z. (Guest)
on 2006-02-23 23:13
(Received via mailing list)
On Feb 23, 2006, at 9:01 AM, Filip G. wrote:

>
> <%
>   code
> %>
> <%= "whatever you want to write" %>
> <%
>   more code
> %>
>
> And that is very tedious.
>
> An echo command is so basic, surely RoR has this?


Also instead of concat you can use _erbout if you dont like the
binding part. But I agree with others that its bad fomr to need this
much code in your views. This in not PHP so you dont use echo or
print. But if you must then you can use _erbout like this:

<%
code
_erbout << "Some text"
more code
%>

Cheers-
-Ezra
Alex Y. (Guest)
on 2006-02-24 11:26
(Received via mailing list)
Filip G. wrote:
> #<#<Class:0x384b968>:0x384b548>
That's because if you put it in a helper method, the binding is picked
up from inside the helper method, which knows nothing about the
variables local to the erb script.  If you're going to be using that
technique, the binding() method actually has to be called in the erb.

>
> Sometimes RoR annoys the hell out of me. Why would they deliberately
> 'outlaw' a most basic function?
Because it's wrong to use it more often than it's right, maybe?
Superbiji (Guest)
on 2006-02-24 11:53
(Received via mailing list)
On 2/24/06, Filip G. <removed_email_address@domain.invalid> wrote:
> With an echo command I could do this:
>   code
> %>
> <%= "whatever you want to write" %>
> <%
>   more code
> %>
>
> And that is very tedious.
>
> An echo command is so basic, surely RoR has this?

I also really hate this,
why there is no "echo" in rails

But I did this,

<%=
s = ""

code
s = s + "append something"
code

s
%>




--
Jual Beli Gratis egold 1$ http://shegold.com/
This topic is locked and can not be replied to.