# Newbie question about .rhtml

**URL:** <https://www.ruby-forum.com/t/newbie-question-about-rhtml/99513>\
**Category:** Rails\
**Created:** [July 23, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/newbie-question-about-rhtml/99513 "2006-07-23T18:08:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Daniel\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Jean-Daniel\_B](https://www.ruby-forum.com/u/Jean-Daniel_B)\
**Post date:** [July 23, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/newbie-question-about-rhtml/99513/1 "2006-07-23T18:08:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jean-Daniel\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Jean-Daniel\_B](https://www.ruby-forum.com/u/Jean-Daniel_B)\
**Post date:** [July 23, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/newbie-question-about-rhtml/99513/2 "2006-07-23T18:17:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jean-Daniel\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Jean-Daniel\_B](https://www.ruby-forum.com/u/Jean-Daniel_B)\
**Post date:** [July 24, 2006, 11:16am UTC](https://www.ruby-forum.com/t/newbie-question-about-rhtml/99513/3 "2006-07-24T11:16:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jean-Daniel\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Jean-Daniel\_B](https://www.ruby-forum.com/u/Jean-Daniel_B)\
**Post date:** [July 24, 2006, 11:22am UTC](https://www.ruby-forum.com/t/newbie-question-about-rhtml/99513/4 "2006-07-24T11:22:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jean-Daniel\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Jean-Daniel\_B](https://www.ruby-forum.com/u/Jean-Daniel_B)\
**Post date:** [July 25, 2006, 8:47am UTC](https://www.ruby-forum.com/t/newbie-question-about-rhtml/99513/5 "2006-07-25T08:47:16Z")

</div>

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](http://railsexpress.de/blog/articles/2005/12/06/trim-your-output)

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000
