Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Serve alternate template formats
Browse files Browse the repository at this point in the history
  • Loading branch information
sstephenson committed Oct 27, 2010
1 parent ef8973d commit 905e871
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
23 changes: 11 additions & 12 deletions lib/brochure/application.rb
Expand Up @@ -47,8 +47,8 @@ def plugins
def call(env)
if forbidden?(env["PATH_INFO"])
forbidden
elsif template = find_template(env["PATH_INFO"][/[^.]+/])
success render_template(template, env)
elsif template = find_template(env["PATH_INFO"])
success render_template(template, env), template.content_type
else
not_found(env)
end
Expand All @@ -58,27 +58,26 @@ def forbidden?(path)
path[".."] || File.basename(path)[/^_/]
end

def find_template(logical_path)
if template_path = find_template_path(logical_path)
def find_template(logical_path, format_extension = ".html")
if template_path = find_template_path(logical_path, format_extension)
template_for(template_path)
end
end

def find_partial(logical_path, format_extension)
def find_partial(logical_path, format_extension = ".html")
if template_path = find_partial_path(logical_path, format_extension)
template_for(template_path)
end
end

def find_template_path(logical_path)
candidates = [logical_path + ".html", logical_path + "/index.html"]
template_trail.find(*candidates)
def find_template_path(logical_path, format_extension)
template_trail.find(logical_path, logical_path + format_extension, logical_path + "/index" + format_extension)
end

def find_partial_path(logical_path, format_extension)
dirname, basename = File.split(logical_path)
partial_path = File.join(dirname, "_" + basename + format_extension)
template_trail.find(partial_path)
partial_path = File.join(dirname, "_" + basename)
template_trail.find(partial_path, partial_path + format_extension)
end

def template_for(template_path)
Expand All @@ -101,8 +100,8 @@ def respond_with(status, body, content_type = "text/html; charset=utf-8")
[status, headers, [body]]
end

def success(body)
respond_with 200, body
def success(body, content_type)
respond_with 200, body, content_type
end

def not_found(env)
Expand Down
5 changes: 5 additions & 0 deletions lib/brochure/template.rb
Expand Up @@ -27,5 +27,10 @@ def format_extension
ext = File.extname(File.basename(path, engine_extension))
ext.empty? ? ".html" : ext
end

def content_type
type = Rack::Mime.mime_type(format_extension)
type[/^text/] ? "#{type}; charset=utf-8" : type
end
end
end
1 change: 1 addition & 0 deletions test/fixtures/default/templates/hello.js.erb
@@ -0,0 +1 @@
var domain = "<%= @domain %>";
7 changes: 7 additions & 0 deletions test/test_brochure.rb
Expand Up @@ -21,6 +21,7 @@ def test_templates_are_rendered_when_present
get "/signup"
assert last_response.ok?
assert_equal "<h1>Sign up</h1>", last_response.body.strip
assert_equal "text/html; charset=utf-8", last_response.content_type
end

def test_index_templates_are_rendered_for_directories
Expand Down Expand Up @@ -113,4 +114,10 @@ def test_assigns_are_available_in_templates
get "/hello"
assert last_response.body['<a href="http://37signals.com/">Home</a>']
end

def test_alternate_template_format
get "/hello.js"
assert last_response.body['var domain = "37signals.com";']
assert_equal "application/javascript", last_response.content_type
end
end

0 comments on commit 905e871

Please sign in to comment.