# Layout for static pages?

**URL:** <https://www.ruby-forum.com/t/layout-for-static-pages/69135>\
**Category:** Rails\
**Created:** [September 18, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/layout-for-static-pages/69135 "2006-09-18T22:43:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [September 18, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/layout-for-static-pages/69135/1 "2006-09-18T22:43:05Z")

</div>

Rails people,

Suppose I have an HTML file in public/ :

 I am static.html

And suppose I have a layout file in app/views/layouts/ :

 I am layout\_for\_static.rhtml \<%= @content\_for\_layout %\> 

|
| |
|
| |

I’m trying to figure out a way to get the static  
file to act as a template so that I get it loaded  
into @content\_for\_layout

Anyone have any thoughts on this ?

I’m thinking I need to write a before\_filter in application.rb  
and maybe work a bit with config/routes.rb

Anyone have any thoughts on this ?

-Dan

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [September 19, 2006, 9:12am UTC](https://www.ruby-forum.com/t/layout-for-static-pages/69135/2 "2006-09-19T09:12:15Z")

</div>

ok,

I wrote something that is about 80% there.

idea 1:  
script/generate controller serve\_static index

2:  
copy all the static content here:  
app/views/serve\_static

3:  
add a line to the END of routes.rb

map.connect ‘\*anything’, :controller =\> “serve\_static” , :action =\>  
‘index’

1. 

Edit index()  
inside of  
app/controllers/serve\_static\_controller.rb

All the info I need is inside of request.request\_uri

Just two lines gets me 50% there:

def index  
the\_file\_name = ‘serve\_static’ + request.request\_uri  
render(:file =\> the\_file\_name, :layout =\> true, :use\_full\_path  
=\>true,  
:content\_type =\> “text/html”)  
end

1. 

Get more elaborate:  
-look at file suffixes to guess about content\_type: jpg, txt, html,  
xml…  
-rescue if I make a bad assumption about file existence

Result:

class ServeStaticController \< ApplicationController

def index  
logger.info 'request.request\_uri is ’  
logger.info request.request\_uri.inspect

```
# get the file name from the request_uri
the_file_name = 'serve_static' + request.request_uri
logger.info 'the_file_name is '
logger.info the_file_name

# Look for something like this:
# /some/path//gna.rl-e_y#%jnk/-/file.whatever
# We want to know if whatever exists and if so, what it is.
regexp = /(^\/.+)(\.\w+$)/

the_match = regexp.match request.request_uri

logger.info 'the_match.to_a.inspect is '
logger.info the_match.to_a.inspect

case
when the_match.to_a[2] == nil
  # No file extension so maybe we have index.html there
  begin
    render(:file => the_file_name + '/index.html' , :layout => true,

```

:use\_full\_path =\>true)  
rescue  
# If nothing there, just render index.rhtml  
render  
end  
else  
logger.info 'the suffix is ’ + the\_match.to\_a[2]  
begin  
case  
when the\_match.to\_a[2] == ‘.jpg’  
render(:file =\> the\_file\_name, :layout =\> false,  
:use\_full\_path  
=\>true, :content\_type =\> “image/jpg”)  
when the\_match.to\_a[2] == ‘.gif’  
render(:file =\> the\_file\_name, :layout =\> false,  
:use\_full\_path  
=\>true, :content\_type =\> “image/gif”)  
when the\_match.to\_a[2] == ‘.png’  
render(:file =\> the\_file\_name, :layout =\> false,  
:use\_full\_path  
=\>true, :content\_type =\> “image/png”)  
when the\_match.to\_a[2] == ‘.txt’  
render(:file =\> the\_file\_name, :layout =\> true,  
:use\_full\_path  
=\>true, :content\_type =\> “text/text”)  
when the\_match.to\_a[2] == ‘.xml’  
render(:file =\> the\_file\_name, :layout =\> true,  
:use\_full\_path  
=\>true, :content\_type =\> “text/xml”)  
else  
render(:file =\> the\_file\_name, :layout =\> true,  
:use\_full\_path  
=\>true, :content\_type =\> “text/html”)  
end  
rescue  
# If nothing there, just render index.rhtml  
render  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [September 19, 2006, 1:15am UTC](https://www.ruby-forum.com/t/layout-for-static-pages/69135/3 "2006-09-19T01:15:21Z")

</div>

The statement,

You simply cannot have anything in /public that needs rails interaction.

is of great help.

So, my approach will probably be:

script/generate controller serve\_static

Then tar-copy my tree of static .html, .htm, .txt, .jpg, .gif …  
to:  
app/views/serve\_static

Then put this in  
config/routes.rb :

# You can have the root of your site routed by hooking up ‘’

# – just remember to delete public/index.html.

map.connect ‘’, :controller =\> “serve\_static”

Next, I need to figure out how to deal with an arbitrary URL.

For example:

/I/am/just\_some/page.txt  
/me/too.html  
/ya/Im/a/picture.jpg  
/mr/htm/here.htm  
/Iam/just/slash/so/call/indexhtml/  
/Im/not/even/slash

I know that I’ll have access to  
request.request\_uri

So, I’m smelling a case statement with 8 whens  
based on  
.txt  
.html  
.jpg  
.gif  
.png  
.htm  
/  
“”

Let’s say I know that I want to render a .txt file,  
how do I actually render it?

I guess I should take a close look at render()  
or any of its cousins.

Any thoughts anyone?

Thanks!

-Dan

---------- Forwarded message ----------  
From: Dan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Sep 18, 2006 1:39 PM  
Subject: Layout for static pages?  
To: removed\_email\_address@domain.invalid

Rails people,

Suppose I have an HTML file in public/ :

 I am static.html

And suppose I have a layout file in app/views/layouts/ :

 I am layout\_for\_static.rhtml \<%= @content\_for\_layout %\> 

|
| |
|
| |

I’m trying to figure out a way to get the static  
file to act as a template so that I get it loaded  
into @content\_for\_layout

Anyone have any thoughts on this ?

I’m thinking I need to write a before\_filter in application.rb  
and maybe work a bit with config/routes.rb

Anyone have any thoughts on this ?

-Dan

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [September 19, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/layout-for-static-pages/69135/4 "2006-09-19T20:00:48Z")

</div>

My end-users can write simple HTML and .txt files .  
They want me to supply layouts, and other webmaster skills.

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [September 19, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/layout-for-static-pages/69135/5 "2006-09-19T18:34:28Z")

</div>

“Dan B.” wrote

> I wrote something that is about 80% there.

[snip]

Just curious. Why would one want to do this?

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [September 19, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/layout-for-static-pages/69135/6 "2006-09-19T22:39:09Z")

</div>

One note - it may be a little icky, but you could revert back to the  
old-style  
idea of server-side includes for layouts, using ERB directly. Check out  
this

[http://www.brainbag.net/blog/articles/2005/12/22/using-erb-rhtml-templates-on-dreamhost](http://www.brainbag.net/blog/articles/2005/12/22/using-erb-rhtml-templates-on-dreamhost)

for an idea of how to make it work. Essentially, you would use ERB in  
the  
files  
in /public to include headers/footers on the page. Getting shtml and PHP  
flashbacks…

On 9/19/06, Dan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > I wrote something that is about 80% there.
> > 
> > [snip]
> > 
> > Just curious. Why would one want to do this?

–  
Matt J.  
removed\_email\_address@domain.invalid  
President/Technical Director, Acme Art Company ([acmeartco.org](http://acmeartco.org))
