diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 315dc2b0..be3ea213 100755 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -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) diff --git a/lib/middleman/features/maruku.rb b/lib/middleman/features/maruku.rb index b3d35b69..4673f67b 100755 --- a/lib/middleman/features/maruku.rb +++ b/lib/middleman/features/maruku.rb @@ -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 diff --git a/lib/middleman/haml.rb b/lib/middleman/haml.rb index d5583adb..3449fb2f 100755 --- a/lib/middleman/haml.rb +++ b/lib/middleman/haml.rb @@ -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 << "
Backtrace: #{e.backtrace.join("\n")}
" diff --git a/lib/middleman/sass.rb b/lib/middleman/sass.rb index d85c96d1..6832e0a6 100755 --- a/lib/middleman/sass.rb +++ b/lib/middleman/sass.rb @@ -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 diff --git a/spec/fixtures/sample/views/custom-layout.html.haml b/spec/fixtures/sample/views/custom-layout.html.haml new file mode 100755 index 00000000..0582dd93 --- /dev/null +++ b/spec/fixtures/sample/views/custom-layout.html.haml @@ -0,0 +1 @@ +%h1 Welcome \ No newline at end of file diff --git a/spec/fixtures/sample/views/custom.haml b/spec/fixtures/sample/views/custom.haml new file mode 100644 index 00000000..87de0ed3 --- /dev/null +++ b/spec/fixtures/sample/views/custom.haml @@ -0,0 +1,5 @@ +%html + %head + %title Custom Layout + %body + = yield \ No newline at end of file diff --git a/spec/page_alias_and_layouts_spec.rb b/spec/page_alias_and_layouts_spec.rb new file mode 100644 index 00000000..b6269a04 --- /dev/null +++ b/spec/page_alias_and_layouts_spec.rb @@ -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 \ No newline at end of file