# Php include() equivalent

**URL:** <https://www.ruby-forum.com/t/php-include-equivalent/102298>\
**Category:** Rails\
**Created:** [August 14, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/php-include-equivalent/102298 "2006-08-14T22:56:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 14, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/php-include-equivalent/102298/1 "2006-08-14T22:56:56Z")

</div>

Is there an equivelent ruby function that is similar or exactly the same  
as the php include() function? I would like to be able to include a  
header for my RoR web application that refers to a url. Thanks!

-Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 14, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/php-include-equivalent/102298/2 "2006-08-14T23:21:43Z")

</div>

Gilles wrote:

> Is there an equivelent ruby function that is similar or exactly the same  
> as the php include() function? I would like to be able to include a  
> header for my RoR web application that refers to a url. Thanks!
> 
> -Gilles

It’s not quite equivalent in rails, simply beacause in PHP all files are  
view files and you may be including functionality, html output, or both.

But it sounds like what you need are partials or layouts.

Layouts:  
Create a file called app/views/layouts/application.rhtml and stick your  
application layout there. Should look like an expanded form of  
something like this:

 Foo ![]() \<%= yield %\> 

Now the content rendered by your actions will be inserted into the \<%=  
yield %\> tag, for all pages, all the time.

Partials:  
create a view file called app/views/some\_controller/\_header.rhtml with  
whatever html/erb fragments you like and render it into your view with:

\<%= render :partial =\> ‘header’ %\>

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 15, 2006, 12:24am UTC](https://www.ruby-forum.com/t/php-include-equivalent/102298/3 "2006-08-15T00:24:36Z")

</div>

Thanks for replying Alex. I understand the idea of layouts and partials  
and have already been using them for several things within my RoR web  
application. However, the problem is that I want to refer to html  
outside of my layout or partial by referencing a URL. This might not be  
entirely necessary, but while we’re on the topic, I wonder if Ruby is  
capable of doing it.

-Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 15, 2006, 1:51am UTC](https://www.ruby-forum.com/t/php-include-equivalent/102298/4 "2006-08-15T01:51:16Z")

</div>

Gilles wrote:

> Thanks for replying Alex. I understand the idea of layouts and partials  
> and have already been using them for several things within my RoR web  
> application. However, the problem is that I want to refer to html  
> outside of my layout or partial by referencing a URL. This might not be  
> entirely necessary, but while we’re on the topic, I wonder if Ruby is  
> capable of doing it.
> 
> -Gilles

Ah, OK.

To get a page on another server, use open-uri or net/http. open-uri is  
easier.

Learn to use it here:  
[http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/)

You basically can download a page into an instance variable in your  
controller (or model) and include in your view like any other instance  
variable.
