Skip to content

Instantly share code, notes, and snippets.

@bhauman
Created January 14, 2010 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhauman/277331 to your computer and use it in GitHub Desktop.
Save bhauman/277331 to your computer and use it in GitHub Desktop.
!!!
%html
%head
%style(type="text/css")
:plain
body {
color: blue;
background: yellow;
}
%body
%h1 Blue Yellow Template
= content
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
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
!!!
%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