Layout for static pages?

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

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

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’

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

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](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

"Dan Bikle" wrote

I wrote something that is about 80% there.

[snip]

Just curious. Why would one want to do this?

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

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

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…