Created
September 25, 2008 04:33
-
-
Save anonymous/12755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Throw your "static" views in app/views/static... and it just works, page cached and all. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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