From 3ce5adac3c4e0c627dfb8187c410d21021c61338 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Fri, 25 Nov 2011 16:09:31 -0800 Subject: [PATCH] change how layouts work --- CHANGELOG | 3 +- features/auto_layout.feature | 37 +++++++++++++++ fixtures/different-engine-layout/config.rb | 0 .../source/index.html.haml | 1 + .../different-engine-layout/source/layout.erb | 9 ++++ fixtures/engine-matching-layout/config.rb | 0 .../source/index.html.erb | 1 + .../engine-matching-layout/source/layout.erb | 9 ++++ fixtures/manual-layout-missing/config.rb | 1 + .../source/index.html.erb | 1 + fixtures/manual-layout-override/config.rb | 3 ++ .../source/index.html.erb | 1 + .../source/layouts/another.erb | 9 ++++ .../source/layouts/custom.erb | 9 ++++ fixtures/manual-layout/config.rb | 1 + fixtures/manual-layout/source/index.html.erb | 1 + .../manual-layout/source/layouts/custom.erb | 9 ++++ fixtures/multiple-layouts/config.rb | 0 .../multiple-layouts/source/index.html.erb | 1 + fixtures/multiple-layouts/source/layout.erb | 9 ++++ fixtures/multiple-layouts/source/layout.haml | 6 +++ fixtures/no-layout/config.rb | 0 fixtures/no-layout/source/index.html.erb | 1 + lib/middleman/base.rb | 17 ++++--- lib/middleman/core_extensions/sitemap.rb | 2 + lib/middleman/sitemap/template.rb | 46 +++++++++++++++---- 26 files changed, 161 insertions(+), 16 deletions(-) create mode 100644 features/auto_layout.feature create mode 100644 fixtures/different-engine-layout/config.rb create mode 100755 fixtures/different-engine-layout/source/index.html.haml create mode 100644 fixtures/different-engine-layout/source/layout.erb create mode 100644 fixtures/engine-matching-layout/config.rb create mode 100755 fixtures/engine-matching-layout/source/index.html.erb create mode 100644 fixtures/engine-matching-layout/source/layout.erb create mode 100644 fixtures/manual-layout-missing/config.rb create mode 100755 fixtures/manual-layout-missing/source/index.html.erb create mode 100644 fixtures/manual-layout-override/config.rb create mode 100755 fixtures/manual-layout-override/source/index.html.erb create mode 100644 fixtures/manual-layout-override/source/layouts/another.erb create mode 100644 fixtures/manual-layout-override/source/layouts/custom.erb create mode 100644 fixtures/manual-layout/config.rb create mode 100755 fixtures/manual-layout/source/index.html.erb create mode 100644 fixtures/manual-layout/source/layouts/custom.erb create mode 100644 fixtures/multiple-layouts/config.rb create mode 100755 fixtures/multiple-layouts/source/index.html.erb create mode 100644 fixtures/multiple-layouts/source/layout.erb create mode 100644 fixtures/multiple-layouts/source/layout.haml create mode 100644 fixtures/no-layout/config.rb create mode 100755 fixtures/no-layout/source/index.html.erb diff --git a/CHANGELOG b/CHANGELOG index 80f38819..0d8a1cc0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,7 +3,8 @@ Rewritten to work directly with Rack (Sinatra apps can still be mounted) Sitemap maintains own state New Extension Registration API -Remove old 1.x mm- binaries +Remove old 1.x mm- binaries and messaging +New default layout functionality: https://github.com/tdreyno/middleman/issues/165 2.1.pre ==== diff --git a/features/auto_layout.feature b/features/auto_layout.feature new file mode 100644 index 00000000..444983ee --- /dev/null +++ b/features/auto_layout.feature @@ -0,0 +1,37 @@ +Feature: Provide Sane Defaults for Layout Behavior + + Scenario: Template and Layout of same engine exist + Given the Server is running at "engine-matching-layout" + When I go to "/index.html" + Then I should see "Comment in layout" + + Scenario: Template and Layout of different engine + Given the Server is running at "different-engine-layout" + When I go to "/index.html" + Then I should see "Comment in layout" + + Scenario: Multiple layouts exist, prefer same engine + Given the Server is running at "multiple-layouts" + When I go to "/index.html" + Then I should see "ERb Comment in layout" + + Scenario: No layout exists + Given the Server is running at "no-layout" + When I go to "/index.html" + Then I should not see "Comment in layout" + + Scenario: Manually set default layout in config (exists) + Given the Server is running at "manual-layout" + When I go to "/index.html" + Then I should see "Custom Comment in layout" + + Scenario: Manually set default layout in config (does not exist) + Given the Server is running at "manual-layout-missing" + When I go to "/index.html" + Then I should see "Error" + + Scenario: Overwrite manual layout + Given the Server is running at "manual-layout-override" + When I go to "/index.html" + Then I should see "Another Comment in layout" + \ No newline at end of file diff --git a/fixtures/different-engine-layout/config.rb b/fixtures/different-engine-layout/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/different-engine-layout/source/index.html.haml b/fixtures/different-engine-layout/source/index.html.haml new file mode 100755 index 00000000..0582dd93 --- /dev/null +++ b/fixtures/different-engine-layout/source/index.html.haml @@ -0,0 +1 @@ +%h1 Welcome \ No newline at end of file diff --git a/fixtures/different-engine-layout/source/layout.erb b/fixtures/different-engine-layout/source/layout.erb new file mode 100644 index 00000000..b72f66e9 --- /dev/null +++ b/fixtures/different-engine-layout/source/layout.erb @@ -0,0 +1,9 @@ + + + My Sample Site + + + + <%= yield %> + + \ No newline at end of file diff --git a/fixtures/engine-matching-layout/config.rb b/fixtures/engine-matching-layout/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/engine-matching-layout/source/index.html.erb b/fixtures/engine-matching-layout/source/index.html.erb new file mode 100755 index 00000000..f47aba51 --- /dev/null +++ b/fixtures/engine-matching-layout/source/index.html.erb @@ -0,0 +1 @@ +

Welcome

\ No newline at end of file diff --git a/fixtures/engine-matching-layout/source/layout.erb b/fixtures/engine-matching-layout/source/layout.erb new file mode 100644 index 00000000..b72f66e9 --- /dev/null +++ b/fixtures/engine-matching-layout/source/layout.erb @@ -0,0 +1,9 @@ + + + My Sample Site + + + + <%= yield %> + + \ No newline at end of file diff --git a/fixtures/manual-layout-missing/config.rb b/fixtures/manual-layout-missing/config.rb new file mode 100644 index 00000000..bfee8fc7 --- /dev/null +++ b/fixtures/manual-layout-missing/config.rb @@ -0,0 +1 @@ +set :layout, :custom \ No newline at end of file diff --git a/fixtures/manual-layout-missing/source/index.html.erb b/fixtures/manual-layout-missing/source/index.html.erb new file mode 100755 index 00000000..f47aba51 --- /dev/null +++ b/fixtures/manual-layout-missing/source/index.html.erb @@ -0,0 +1 @@ +

Welcome

\ No newline at end of file diff --git a/fixtures/manual-layout-override/config.rb b/fixtures/manual-layout-override/config.rb new file mode 100644 index 00000000..6f247735 --- /dev/null +++ b/fixtures/manual-layout-override/config.rb @@ -0,0 +1,3 @@ +set :layout, :custom + +page "/", :layout => :another \ No newline at end of file diff --git a/fixtures/manual-layout-override/source/index.html.erb b/fixtures/manual-layout-override/source/index.html.erb new file mode 100755 index 00000000..f47aba51 --- /dev/null +++ b/fixtures/manual-layout-override/source/index.html.erb @@ -0,0 +1 @@ +

Welcome

\ No newline at end of file diff --git a/fixtures/manual-layout-override/source/layouts/another.erb b/fixtures/manual-layout-override/source/layouts/another.erb new file mode 100644 index 00000000..a32bea98 --- /dev/null +++ b/fixtures/manual-layout-override/source/layouts/another.erb @@ -0,0 +1,9 @@ + + + My Sample Site + + + + <%= yield %> + + \ No newline at end of file diff --git a/fixtures/manual-layout-override/source/layouts/custom.erb b/fixtures/manual-layout-override/source/layouts/custom.erb new file mode 100644 index 00000000..33e42a79 --- /dev/null +++ b/fixtures/manual-layout-override/source/layouts/custom.erb @@ -0,0 +1,9 @@ + + + My Sample Site + + + + <%= yield %> + + \ No newline at end of file diff --git a/fixtures/manual-layout/config.rb b/fixtures/manual-layout/config.rb new file mode 100644 index 00000000..bfee8fc7 --- /dev/null +++ b/fixtures/manual-layout/config.rb @@ -0,0 +1 @@ +set :layout, :custom \ No newline at end of file diff --git a/fixtures/manual-layout/source/index.html.erb b/fixtures/manual-layout/source/index.html.erb new file mode 100755 index 00000000..f47aba51 --- /dev/null +++ b/fixtures/manual-layout/source/index.html.erb @@ -0,0 +1 @@ +

Welcome

\ No newline at end of file diff --git a/fixtures/manual-layout/source/layouts/custom.erb b/fixtures/manual-layout/source/layouts/custom.erb new file mode 100644 index 00000000..33e42a79 --- /dev/null +++ b/fixtures/manual-layout/source/layouts/custom.erb @@ -0,0 +1,9 @@ + + + My Sample Site + + + + <%= yield %> + + \ No newline at end of file diff --git a/fixtures/multiple-layouts/config.rb b/fixtures/multiple-layouts/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/multiple-layouts/source/index.html.erb b/fixtures/multiple-layouts/source/index.html.erb new file mode 100755 index 00000000..f47aba51 --- /dev/null +++ b/fixtures/multiple-layouts/source/index.html.erb @@ -0,0 +1 @@ +

Welcome

\ No newline at end of file diff --git a/fixtures/multiple-layouts/source/layout.erb b/fixtures/multiple-layouts/source/layout.erb new file mode 100644 index 00000000..270ec88b --- /dev/null +++ b/fixtures/multiple-layouts/source/layout.erb @@ -0,0 +1,9 @@ + + + My Sample Site + + + + <%= yield %> + + \ No newline at end of file diff --git a/fixtures/multiple-layouts/source/layout.haml b/fixtures/multiple-layouts/source/layout.haml new file mode 100644 index 00000000..771157f0 --- /dev/null +++ b/fixtures/multiple-layouts/source/layout.haml @@ -0,0 +1,6 @@ +%html + %head + %title My Sample Site + / Haml Comment in layout + %body + = yield \ No newline at end of file diff --git a/fixtures/no-layout/config.rb b/fixtures/no-layout/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/no-layout/source/index.html.erb b/fixtures/no-layout/source/index.html.erb new file mode 100755 index 00000000..f47aba51 --- /dev/null +++ b/fixtures/no-layout/source/index.html.erb @@ -0,0 +1 @@ +

Welcome

\ No newline at end of file diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 9bedc244..273d201f 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -165,7 +165,7 @@ class Middleman::Base ] # Default layout name - set :layout, :layout + set :layout, :_auto_layout # Activate custom features and extensions include Middleman::CoreExtensions::Extensions @@ -328,11 +328,16 @@ class Middleman::Base # Set a HTTP content type based on the request's extensions content_type sitemap_page.mime_type - # Valid content is a 200 status - res.status = 200 - - # Write out the contents of the page - res.write sitemap_page.render + begin + # Write out the contents of the page + res.write sitemap_page.render + + # Valid content is a 200 status + res.status = 200 + rescue ::Middleman::Sitemap::TemplateNotFound => e + res.write "Error: #{e.message}" + res.status = 500 + end # End the request halt res.finish diff --git a/lib/middleman/core_extensions/sitemap.rb b/lib/middleman/core_extensions/sitemap.rb index ac0ff162..41db5ea3 100644 --- a/lib/middleman/core_extensions/sitemap.rb +++ b/lib/middleman/core_extensions/sitemap.rb @@ -11,6 +11,8 @@ module Middleman::CoreExtensions::Sitemap module InstanceMethods def initialize + ::Middleman::Sitemap::Template.cache.clear + file_changed do |file| sitemap.touch_file(file) end diff --git a/lib/middleman/sitemap/template.rb b/lib/middleman/sitemap/template.rb index 4d923bcd..15f3ec63 100644 --- a/lib/middleman/sitemap/template.rb +++ b/lib/middleman/sitemap/template.rb @@ -1,4 +1,7 @@ module Middleman::Sitemap + class TemplateNotFound < RuntimeError + end + class Template attr_accessor :page, :options, :locals, :blocks#, :dependencies @@ -100,14 +103,39 @@ module Middleman::Sitemap engine end - layout_path, *etc = resolve_template(local_layout, :preferred_engine => layout_engine) - - if !layout_path - local_layout = File.join("layouts", local_layout.to_s) - layout_path, *etc = resolve_template(local_layout, :preferred_engine => layout_engine) + # Automatic + if local_layout == :_auto_layout + # Look for :layout of any extension + # If found, use it. If not, continue + locate_layout(:layout, layout_engine) || false + else + # Look for specific layout + # If found, use it. If not, error. + if layout_path = locate_layout(local_layout, layout_engine) + layout_path + else + raise Middleman::Sitemap::TemplateNotFound, "Could not locate layout: #{local_layout}" + end end + end + + def locate_layout(name, preferred_engine=nil) + # Check root + layout_path, *etc = resolve_template(name, :preferred_engine => preferred_engine) - throw "Could not locate layout: #{local_layout}" unless layout_path + # Check layouts folder + if !layout_path + layout_path, *etc = resolve_template(File.join("layouts", name.to_s), :preferred_engine => preferred_engine) + end + + # Check root, no preference + layout_path, *etc = resolve_template(name) + + # Check layouts folder, no preference + if !layout_path + layout_path, *etc = resolve_template(File.join("layouts", name.to_s)) + end + layout_path end @@ -161,9 +189,9 @@ module Middleman::Sitemap def self.static_render(app, path, body, locs = {}, opts = {}, &block) options = opts.merge(options_for_ext(File.extname(path))) - template = cache.fetch(:compiled_template, options, body) do - ::Tilt.new(path, 1, options) { body } - end + # template = cache.fetch(:compiled_template, options, body) do + template = ::Tilt.new(path, 1, options) { body } + # end template.render(app, locs, &block) end