Anonymous (owner)

Forks

Revisions

  • a9cdbf Wed Sep 24 21:50:41 -0700 2008
  • 83835c Wed Sep 24 21:49:14 -0700 2008
  • 165325 Wed Sep 24 21:33:39 -0700 2008
gist: 12755 Download_button fork
public
Public Clone URL: git://gist.github.com/12755.git
Embed All Files: show embed
README #
1
Throw your "static" views in app/views/static... and it just works, page cached and all.
router.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Merb.logger.info("Compiling routes...")
Merb::Router.prepare do
  
  # This is the default route for /:controller/:action/:id
  # This is fine for most cases. If you're heavily using resource-based
  # routes, you may want to comment/remove this line to prevent
  # clients from calling your create or destroy actions with a GET
  default_routes
  
  # Change this for your home page to be available at /
  match('/').to(:controller => 'static', :action =>'show', :path => '/index')
  
  # If a template is found in app/views/static, send it to the page-cached static controller
  # Probably want to add something here to check for an index template in app/views/static/request/path ie. /request/path(/index.html.erb)
  match(/.*/).defer_to do |request, params|
    # See if a template matches for the requested URI in the Static controller, matching any content type
    # Will only route to the static controller if a template is found
    # ie. /foo/bar/template_exists will match
    # /foo/bar/a_template_that_does_not_exist will not match, and will continue through the route matching
    if Merb::Template.template_for(Static._template_root / "static#{request.uri}.*")
      params.merge :controller => 'static', :action => 'show', :path => request.uri
    end
  end
 
end
static.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Static < Application
  cache_page(:show)
  
  def show(path)
    # Mess around with different layouts if you like
    # You can basically match on the path and do anything you like with the render call
    # Could even get it to find layouts based on the path by default. (like controllers do)
    if path =~ /\/help/
      layout = :help
    end
    
    render(:template => "static#{path}.#{content_type}", :layout => layout)
  end
end