page method and with_layout block

This commit is contained in:
tdreyno 2009-11-16 11:32:55 -08:00
parent aa5a3d5918
commit 84c47f3ced
7 changed files with 68 additions and 11 deletions

View file

@ -4,6 +4,10 @@ require 'rubygems' unless ENV['NO_RUBYGEMS']
# We're riding on Sinatra, so let's include it
require 'sinatra/base'
class Sinatra::Request
attr_accessor :layout
end
module Middleman
class Base < Sinatra::Base
set :app_file, __FILE__
@ -53,9 +57,9 @@ module Middleman
# Base case renderer (do nothing), Should be over-ridden
module StaticRender
def render_path(path)
def render_path(path, layout)
if template_exists?(path, :erb)
erb(path.to_sym)
erb(path.to_sym, :layout => layout)
else
false
end
@ -63,8 +67,7 @@ module Middleman
end
include StaticRender
# This will match all requests not overridden in the project's init.rb
not_found do
def process_request
# Normalize the path and add index if we're looking at a directory
path = request.path
path << options.index_file if path.match(%r{/$})
@ -73,7 +76,7 @@ module Middleman
# layout(:"layout.html") # Insert the .html into the layout name like the rest of the templates
# If the enabled renderers succeed, return the content, mime-type and an HTTP 200
if content = render_path(path)
if content = render_path(path, (request.layout || :layout))
content_type media_type(File.extname(path)), :charset => 'utf-8'
status 200
content
@ -81,6 +84,31 @@ module Middleman
status 404
end
end
def self.page(url, options={}, &block)
get(url) do
request.layout = @@layout if (@@layout ||= nil)
request.layout = options[:layout] if options[:layout]
if block_given?
yield
else
process_request
end
end
end
def self.with_layout(layout, &block)
@@layout = layout
class_eval(&block)
ensure
@@layout = nil
end
# This will match all requests not overridden in the project's init.rb
not_found do
process_request
end
end
end
@ -108,11 +136,11 @@ class Middleman::Base
disable :maruku
disable :smush_pngs
disable :automatic_image_sizes
disable :relative_assets
disable :cache_buster
# Default build features
configure :build do
enable :relative_assets
enable :cache_buster
end
def self.new(*args, &bk)

View file

@ -11,7 +11,7 @@ module Middleman
base.set :maruku, {}
end
def render_path(path)
def render_path(path, layout)
if template_exists?(path, :maruku)
render :maruku, path.to_sym
else

View file

@ -8,11 +8,12 @@ module Middleman
base.helpers Middleman::Haml::Helpers
end
def render_path(path)
def render_path(path, layout)
if template_exists?(path, :haml)
result = nil
begin
result = haml(path.to_sym, :layout => File.extname(path) != ".xml")
layout = false if File.extname(path) == ".xml"
result = haml(path.to_sym, :layout => layout)
rescue ::Haml::Error => e
result = "Haml Error: #{e}"
result << "<pre>Backtrace: #{e.backtrace.join("\n")}</pre>"

View file

@ -8,7 +8,7 @@ module Middleman
base.supported_formats << "sass"
end
def render_path(path)
def render_path(path, layout)
if template_exists?(path, :sass)
begin
static_version = options.public + request.path_info

View file

@ -0,0 +1 @@
%h1 Welcome

View file

@ -0,0 +1,5 @@
%html
%head
%title Custom Layout
%body
= yield

View file

@ -0,0 +1,22 @@
require File.join(File.dirname(__FILE__), "spec_helper")
base = ::Middleman::Base
base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample")
describe "Custom layout" do
it "should use custom layout" do
base.page "/custom-layout.html", :layout => :custom
browser = Rack::Test::Session.new(Rack::MockSession.new(base.new))
browser.get("/custom-layout.html")
browser.last_response.body.should include("Custom Layout")
end
it "should use custom layout with_layout method" do
base.with_layout :layout => :custom do
page "/custom-layout.html"
end
browser = Rack::Test::Session.new(Rack::MockSession.new(base.new))
browser.get("/custom-layout.html")
browser.last_response.body.should include("Custom Layout")
end
end