# How to output something from within \<% %\> tags?

**URL:** <https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121>\
**Category:** Rails\
**Created:** [February 23, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121 "2006-02-23T17:29:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![godsmurf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@godsmurf](https://www.ruby-forum.com/u/godsmurf)\
**Post date:** [February 23, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121/1 "2006-02-23T17:29:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![godsmurf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@godsmurf](https://www.ruby-forum.com/u/godsmurf)\
**Post date:** [February 23, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121/2 "2006-02-23T17:39:21Z")

</div>

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” %\>

---

<div class="post-metadata">

**Author:** ![godsmurf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@godsmurf](https://www.ruby-forum.com/u/godsmurf)\
**Post date:** [February 23, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121/3 "2006-02-23T17:48:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![godsmurf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@godsmurf](https://www.ruby-forum.com/u/godsmurf)\
**Post date:** [February 23, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121/4 "2006-02-23T18:15:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![godsmurf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@godsmurf](https://www.ruby-forum.com/u/godsmurf)\
**Post date:** [February 23, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121/5 "2006-02-23T18:01:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![godsmurf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@godsmurf](https://www.ruby-forum.com/u/godsmurf)\
**Post date:** [February 23, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121/6 "2006-02-23T18:40:16Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![godsmurf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@godsmurf](https://www.ruby-forum.com/u/godsmurf)\
**Post date:** [February 24, 2006, 10:26am UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121/7 "2006-02-24T10:26:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![godsmurf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@godsmurf](https://www.ruby-forum.com/u/godsmurf)\
**Post date:** [February 24, 2006, 10:53am UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121/8 "2006-02-24T10:53:44Z")

</div>

On 2/24/06, Filip G. [removed\_email\_address@domain.invalid](mailto: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/](http://shegold.com/)

---

<div class="post-metadata">

**Author:** ![godsmurf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@godsmurf](https://www.ruby-forum.com/u/godsmurf)\
**Post date:** [February 23, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/how-to-output-something-from-within-tags/54121/9 "2006-02-23T22:13:17Z")

</div>

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
