Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2008 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/12755 to your computer and use it in GitHub Desktop.
Save anonymous/12755 to your computer and use it in GitHub Desktop.
Throw your "static" views in app/views/static... and it just works, page cached and all.
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment