# Layout for static pages?

**URL:** <https://discuss.rubyonrails.org/t/layout-for-static-pages/1815>\
**Category:** rubyonrails-talk\
**Created:** [September 18, 2006, 8:39pm UTC](https://discuss.rubyonrails.org/t/layout-for-static-pages/1815 "2006-09-18T20:39:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Bikle1](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@Dan\_Bikle1](https://discuss.rubyonrails.org/u/Dan_Bikle1)\
**Post date:** [September 18, 2006, 8:39pm UTC](https://discuss.rubyonrails.org/t/layout-for-static-pages/1815/1 "2006-09-18T20:39:14Z")

</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:** ![Dan\_Bikle1](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@Dan\_Bikle1](https://discuss.rubyonrails.org/u/Dan_Bikle1)\
**Post date:** [September 18, 2006, 11:10pm UTC](https://discuss.rubyonrails.org/t/layout-for-static-pages/1815/2 "2006-09-18T23:10:39Z")

</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

---

<div class="post-metadata">

**Author:** ![Dan\_Bikle1](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@Dan\_Bikle1](https://discuss.rubyonrails.org/u/Dan_Bikle1)\
**Post date:** [September 19, 2006, 7:06am UTC](https://discuss.rubyonrails.org/t/layout-for-static-pages/1815/3 "2006-09-19T07:06:53Z")

</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](http://logger.info) 'request.request\_uri is ’ [logger.info](http://logger.info) request.request\_uri.inspect

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

[logger.info](http://logger.info) 'the_file_name is '
[logger.info](http://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](http://logger.info) 'the_match.to_a.inspect is '
[logger.info](http://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](http://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:** ![Long](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@Long](https://discuss.rubyonrails.org/u/Long)\
**Post date:** [September 19, 2006, 4:22pm UTC](https://discuss.rubyonrails.org/t/layout-for-static-pages/1815/4 "2006-09-19T16:22:47Z")

</div>

"Dan Bikle" wrote

> I wrote something that is about 80% there.

[snip]

Just curious. Why would one want to do this?

---

<div class="post-metadata">

**Author:** ![Dan\_Bikle1](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@Dan\_Bikle1](https://discuss.rubyonrails.org/u/Dan_Bikle1)\
**Post date:** [September 19, 2006, 5:50pm UTC](https://discuss.rubyonrails.org/t/layout-for-static-pages/1815/5 "2006-09-19T17:50:14Z")

</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:** ![Matt\_Jones1](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@Matt\_Jones1](https://discuss.rubyonrails.org/u/Matt_Jones1)\
**Post date:** [September 19, 2006, 8:34pm UTC](https://discuss.rubyonrails.org/t/layout-for-static-pages/1815/6 "2006-09-19T20:34:49Z")

</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…
