Created
January 14, 2010 17:31
-
-
Save bhauman/277331 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
!!! | |
%html | |
%head | |
%style(type="text/css") | |
:plain | |
body { | |
color: blue; | |
background: yellow; | |
} | |
%body | |
%h1 Blue Yellow Template | |
= content |
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
require 'rubygems' | |
require 'haml' | |
require 'tilt' | |
require 'layout' | |
app = lambda {|env| [200, {'Content-Type' => 'text/html', 'X-Layout' => 'blue_yellow'}, 'Hello World'] } | |
use Layout | |
run app |
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 Layout | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
status, headers, response = @app.call(env) | |
new_response = layout(build_response_body(response), headers) | |
new_headers = recalculate_body_length(headers, new_response) | |
[status, new_headers, new_response] | |
end | |
def layout(response, headers) | |
template = Tilt.new(headers['X-Layout'] + ".haml") | |
template.render(nil, :content => response) | |
end | |
# Borrowed from Rack::Honeypot | |
def build_response_body(response) | |
response_body = "" | |
response.each { |part| response_body += part } | |
response_body | |
end | |
def recalculate_body_length(headers, body) | |
new_headers = headers | |
new_headers["Content-Length"] = body.length.to_s | |
new_headers | |
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
!!! | |
%html | |
%head | |
%style(type="text/css") | |
:plain | |
body { | |
color: red; | |
background: black; | |
} | |
%body | |
%h1 Red Black Template | |
= content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment