change how layouts work

This commit is contained in:
Thomas Reynolds 2011-11-25 16:09:31 -08:00
parent e920cef76a
commit 3ce5adac3c
26 changed files with 161 additions and 16 deletions

View file

@ -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
====

View 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"

View file

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

View file

@ -0,0 +1,9 @@
<html>
<head>
<title>My Sample Site</title>
<!-- Comment in layout -->
</head>
<body>
<%= yield %>
</body>
</html>

View file

@ -0,0 +1 @@
<h1>Welcome</h1>

View file

@ -0,0 +1,9 @@
<html>
<head>
<title>My Sample Site</title>
<!-- Comment in layout -->
</head>
<body>
<%= yield %>
</body>
</html>

View file

@ -0,0 +1 @@
set :layout, :custom

View file

@ -0,0 +1 @@
<h1>Welcome</h1>

View file

@ -0,0 +1,3 @@
set :layout, :custom
page "/", :layout => :another

View file

@ -0,0 +1 @@
<h1>Welcome</h1>

View file

@ -0,0 +1,9 @@
<html>
<head>
<title>My Sample Site</title>
<!-- Another Comment in layout -->
</head>
<body>
<%= yield %>
</body>
</html>

View file

@ -0,0 +1,9 @@
<html>
<head>
<title>My Sample Site</title>
<!-- Custom Comment in layout -->
</head>
<body>
<%= yield %>
</body>
</html>

View file

@ -0,0 +1 @@
set :layout, :custom

View file

@ -0,0 +1 @@
<h1>Welcome</h1>

View file

@ -0,0 +1,9 @@
<html>
<head>
<title>My Sample Site</title>
<!-- Custom Comment in layout -->
</head>
<body>
<%= yield %>
</body>
</html>

View file

View file

@ -0,0 +1 @@
<h1>Welcome</h1>

View file

@ -0,0 +1,9 @@
<html>
<head>
<title>My Sample Site</title>
<!-- ERb Comment in layout -->
</head>
<body>
<%= yield %>
</body>
</html>

View file

@ -0,0 +1,6 @@
%html
%head
%title My Sample Site
/ Haml Comment in layout
%body
= yield

View file

View file

@ -0,0 +1 @@
<h1>Welcome</h1>

View file

@ -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
begin
# 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
halt res.finish

View file

@ -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

View file

@ -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)
# 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)
# Check layouts folder
if !layout_path
local_layout = File.join("layouts", local_layout.to_s)
layout_path, *etc = resolve_template(local_layout, :preferred_engine => layout_engine)
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
throw "Could not locate layout: #{local_layout}" unless layout_path
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