Create a gist now

Instantly share code, notes, and snippets.

anonymous /README
Created Sep 25, 2008

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