change how layouts work
This commit is contained in:
parent
e920cef76a
commit
3ce5adac3c
|
@ -3,7 +3,8 @@
|
||||||
Rewritten to work directly with Rack (Sinatra apps can still be mounted)
|
Rewritten to work directly with Rack (Sinatra apps can still be mounted)
|
||||||
Sitemap maintains own state
|
Sitemap maintains own state
|
||||||
New Extension Registration API
|
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
|
2.1.pre
|
||||||
====
|
====
|
||||||
|
|
37
features/auto_layout.feature
Normal file
37
features/auto_layout.feature
Normal file
|
@ -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"
|
||||||
|
|
0
fixtures/different-engine-layout/config.rb
Normal file
0
fixtures/different-engine-layout/config.rb
Normal file
1
fixtures/different-engine-layout/source/index.html.haml
Executable file
1
fixtures/different-engine-layout/source/index.html.haml
Executable file
|
@ -0,0 +1 @@
|
||||||
|
%h1 Welcome
|
9
fixtures/different-engine-layout/source/layout.erb
Normal file
9
fixtures/different-engine-layout/source/layout.erb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>My Sample Site</title>
|
||||||
|
<!-- Comment in layout -->
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<%= yield %>
|
||||||
|
</body>
|
||||||
|
</html>
|
0
fixtures/engine-matching-layout/config.rb
Normal file
0
fixtures/engine-matching-layout/config.rb
Normal file
1
fixtures/engine-matching-layout/source/index.html.erb
Executable file
1
fixtures/engine-matching-layout/source/index.html.erb
Executable file
|
@ -0,0 +1 @@
|
||||||
|
<h1>Welcome</h1>
|
9
fixtures/engine-matching-layout/source/layout.erb
Normal file
9
fixtures/engine-matching-layout/source/layout.erb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>My Sample Site</title>
|
||||||
|
<!-- Comment in layout -->
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<%= yield %>
|
||||||
|
</body>
|
||||||
|
</html>
|
1
fixtures/manual-layout-missing/config.rb
Normal file
1
fixtures/manual-layout-missing/config.rb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
set :layout, :custom
|
1
fixtures/manual-layout-missing/source/index.html.erb
Executable file
1
fixtures/manual-layout-missing/source/index.html.erb
Executable file
|
@ -0,0 +1 @@
|
||||||
|
<h1>Welcome</h1>
|
3
fixtures/manual-layout-override/config.rb
Normal file
3
fixtures/manual-layout-override/config.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
set :layout, :custom
|
||||||
|
|
||||||
|
page "/", :layout => :another
|
1
fixtures/manual-layout-override/source/index.html.erb
Executable file
1
fixtures/manual-layout-override/source/index.html.erb
Executable file
|
@ -0,0 +1 @@
|
||||||
|
<h1>Welcome</h1>
|
|
@ -0,0 +1,9 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>My Sample Site</title>
|
||||||
|
<!-- Another Comment in layout -->
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<%= yield %>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,9 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>My Sample Site</title>
|
||||||
|
<!-- Custom Comment in layout -->
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<%= yield %>
|
||||||
|
</body>
|
||||||
|
</html>
|
1
fixtures/manual-layout/config.rb
Normal file
1
fixtures/manual-layout/config.rb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
set :layout, :custom
|
1
fixtures/manual-layout/source/index.html.erb
Executable file
1
fixtures/manual-layout/source/index.html.erb
Executable file
|
@ -0,0 +1 @@
|
||||||
|
<h1>Welcome</h1>
|
9
fixtures/manual-layout/source/layouts/custom.erb
Normal file
9
fixtures/manual-layout/source/layouts/custom.erb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>My Sample Site</title>
|
||||||
|
<!-- Custom Comment in layout -->
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<%= yield %>
|
||||||
|
</body>
|
||||||
|
</html>
|
0
fixtures/multiple-layouts/config.rb
Normal file
0
fixtures/multiple-layouts/config.rb
Normal file
1
fixtures/multiple-layouts/source/index.html.erb
Executable file
1
fixtures/multiple-layouts/source/index.html.erb
Executable file
|
@ -0,0 +1 @@
|
||||||
|
<h1>Welcome</h1>
|
9
fixtures/multiple-layouts/source/layout.erb
Normal file
9
fixtures/multiple-layouts/source/layout.erb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>My Sample Site</title>
|
||||||
|
<!-- ERb Comment in layout -->
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<%= yield %>
|
||||||
|
</body>
|
||||||
|
</html>
|
6
fixtures/multiple-layouts/source/layout.haml
Normal file
6
fixtures/multiple-layouts/source/layout.haml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
%html
|
||||||
|
%head
|
||||||
|
%title My Sample Site
|
||||||
|
/ Haml Comment in layout
|
||||||
|
%body
|
||||||
|
= yield
|
0
fixtures/no-layout/config.rb
Normal file
0
fixtures/no-layout/config.rb
Normal file
1
fixtures/no-layout/source/index.html.erb
Executable file
1
fixtures/no-layout/source/index.html.erb
Executable file
|
@ -0,0 +1 @@
|
||||||
|
<h1>Welcome</h1>
|
|
@ -165,7 +165,7 @@ class Middleman::Base
|
||||||
]
|
]
|
||||||
|
|
||||||
# Default layout name
|
# Default layout name
|
||||||
set :layout, :layout
|
set :layout, :_auto_layout
|
||||||
|
|
||||||
# Activate custom features and extensions
|
# Activate custom features and extensions
|
||||||
include Middleman::CoreExtensions::Extensions
|
include Middleman::CoreExtensions::Extensions
|
||||||
|
@ -328,11 +328,16 @@ class Middleman::Base
|
||||||
# Set a HTTP content type based on the request's extensions
|
# Set a HTTP content type based on the request's extensions
|
||||||
content_type sitemap_page.mime_type
|
content_type sitemap_page.mime_type
|
||||||
|
|
||||||
# Valid content is a 200 status
|
begin
|
||||||
res.status = 200
|
# Write out the contents of the page
|
||||||
|
res.write sitemap_page.render
|
||||||
# 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
|
# End the request
|
||||||
halt res.finish
|
halt res.finish
|
||||||
|
|
|
@ -11,6 +11,8 @@ module Middleman::CoreExtensions::Sitemap
|
||||||
|
|
||||||
module InstanceMethods
|
module InstanceMethods
|
||||||
def initialize
|
def initialize
|
||||||
|
::Middleman::Sitemap::Template.cache.clear
|
||||||
|
|
||||||
file_changed do |file|
|
file_changed do |file|
|
||||||
sitemap.touch_file(file)
|
sitemap.touch_file(file)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
module Middleman::Sitemap
|
module Middleman::Sitemap
|
||||||
|
class TemplateNotFound < RuntimeError
|
||||||
|
end
|
||||||
|
|
||||||
class Template
|
class Template
|
||||||
attr_accessor :page, :options, :locals, :blocks#, :dependencies
|
attr_accessor :page, :options, :locals, :blocks#, :dependencies
|
||||||
|
|
||||||
|
@ -100,14 +103,39 @@ module Middleman::Sitemap
|
||||||
engine
|
engine
|
||||||
end
|
end
|
||||||
|
|
||||||
layout_path, *etc = resolve_template(local_layout, :preferred_engine => layout_engine)
|
# Automatic
|
||||||
|
if local_layout == :_auto_layout
|
||||||
if !layout_path
|
# Look for :layout of any extension
|
||||||
local_layout = File.join("layouts", local_layout.to_s)
|
# If found, use it. If not, continue
|
||||||
layout_path, *etc = resolve_template(local_layout, :preferred_engine => layout_engine)
|
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
|
||||||
|
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
|
layout_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -161,9 +189,9 @@ module Middleman::Sitemap
|
||||||
def self.static_render(app, path, body, locs = {}, opts = {}, &block)
|
def self.static_render(app, path, body, locs = {}, opts = {}, &block)
|
||||||
options = opts.merge(options_for_ext(File.extname(path)))
|
options = opts.merge(options_for_ext(File.extname(path)))
|
||||||
|
|
||||||
template = cache.fetch(:compiled_template, options, body) do
|
# template = cache.fetch(:compiled_template, options, body) do
|
||||||
::Tilt.new(path, 1, options) { body }
|
template = ::Tilt.new(path, 1, options) { body }
|
||||||
end
|
# end
|
||||||
|
|
||||||
template.render(app, locs, &block)
|
template.render(app, locs, &block)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue