# Include contents of a file in a view

**URL:** <https://www.ruby-forum.com/t/include-contents-of-a-file-in-a-view/108738>\
**Category:** Rails\
**Created:** [July 6, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/include-contents-of-a-file-in-a-view/108738 "2007-07-06T13:26:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [July 6, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/include-contents-of-a-file-in-a-view/108738/1 "2007-07-06T13:26:22Z")

</div>

Guys,

I have an application and part of its function is to display html  
marked up text files to the users, inlined in the current page  
layout. I have the files stored outside the Rails app directories,  
and not in the database.

What I want to do is something like this in my view:

# Viewing contents of filename
\<%= send\_file( ... ) -%\>

So that my normal layout gets put around the included file.

I know I could easily create a helper, say, my\_send\_file that takes  
the filename and returns its contents, but it means the entire file  
will be pulled into memory (the files could be a reasonable size, but  
not massive!). I guess if the files were in the database I would have  
to pull the data out of the database into memory anyway, making this  
an equivalent process.

Basically I want to know

1. Is there a builtin way to do this, or will I just go ahead and  
create a helper (I don’t think the send\_file method is going to be any  
good for me here).
2. When rendering a large page, does Rails stream it to the user, or  
does it build it all in memory, and then stream it? Just curious on  
this one …

Thanks,

Stephen.

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [July 6, 2007, 2:28pm UTC](https://www.ruby-forum.com/t/include-contents-of-a-file-in-a-view/108738/2 "2007-07-06T14:28:27Z")

</div>

Open the file, iterate through the lines and parse it with CodeRay or  
something similar to get syntax highlighting.

File.open(“the\_filename.html”, “r”).each { |line| puts line }

Etc.

Not sure about the 2nd one. I’m pretty sure it keeps the page in  
memory until it’s sent to the user, then removes it from memory - at  
least that’s what makes most sense.

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [July 6, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/include-contents-of-a-file-in-a-view/108738/3 "2007-07-06T18:21:10Z")

</div>

I’m almost 100% it’s not possible to read an entire file to send to  
the user without reading it into memory first. But you shouldn’t be  
worried about that, memory is fast and I’m assuming your files are  
less than 1MB (since they are HTML). If they are bigger than 1MB, then  
you should be worried about bandwidth and browsers rendering 1MB of  
HTML.

I hope that helps.

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [July 6, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/include-contents-of-a-file-in-a-view/108738/4 "2007-07-06T19:04:19Z")

</div>

On Jul 6, 1:27 pm, August L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > an equivalent process.  
> > Thanks,
> 
> > Stephen.

Yea thats pretty much what I did in a help.

Cheers for the hint on Coderay - that may be useful for some of the  
stuff I need to do!

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [July 6, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/include-contents-of-a-file-in-a-view/108738/5 "2007-07-06T19:06:19Z")

</div>

On Jul 6, 5:20 pm, AryaAsemanfar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m almost 100% it’s not possible to read an entire file to send to  
> the user without reading it into memory first. But you shouldn’t be  
> worried about that, memory is fast and I’m assuming your files are  
> less than 1MB (since they are HTML). If they are bigger than 1MB, then  
> you should be worried about bandwidth and browsers rendering 1MB of  
> HTML.
> 
> I hope that helps.

Reckoned that was the case - cheers for the input, thought it would be  
worth checking just incase!
