page method and with_layout block
This commit is contained in:
parent
aa5a3d5918
commit
84c47f3ced
|
@ -4,6 +4,10 @@ require 'rubygems' unless ENV['NO_RUBYGEMS']
|
||||||
# We're riding on Sinatra, so let's include it
|
# We're riding on Sinatra, so let's include it
|
||||||
require 'sinatra/base'
|
require 'sinatra/base'
|
||||||
|
|
||||||
|
class Sinatra::Request
|
||||||
|
attr_accessor :layout
|
||||||
|
end
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
class Base < Sinatra::Base
|
class Base < Sinatra::Base
|
||||||
set :app_file, __FILE__
|
set :app_file, __FILE__
|
||||||
|
@ -53,9 +57,9 @@ module Middleman
|
||||||
|
|
||||||
# Base case renderer (do nothing), Should be over-ridden
|
# Base case renderer (do nothing), Should be over-ridden
|
||||||
module StaticRender
|
module StaticRender
|
||||||
def render_path(path)
|
def render_path(path, layout)
|
||||||
if template_exists?(path, :erb)
|
if template_exists?(path, :erb)
|
||||||
erb(path.to_sym)
|
erb(path.to_sym, :layout => layout)
|
||||||
else
|
else
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
@ -63,8 +67,7 @@ module Middleman
|
||||||
end
|
end
|
||||||
include StaticRender
|
include StaticRender
|
||||||
|
|
||||||
# This will match all requests not overridden in the project's init.rb
|
def process_request
|
||||||
not_found do
|
|
||||||
# Normalize the path and add index if we're looking at a directory
|
# Normalize the path and add index if we're looking at a directory
|
||||||
path = request.path
|
path = request.path
|
||||||
path << options.index_file if path.match(%r{/$})
|
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
|
# 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 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'
|
content_type media_type(File.extname(path)), :charset => 'utf-8'
|
||||||
status 200
|
status 200
|
||||||
content
|
content
|
||||||
|
@ -81,6 +84,31 @@ module Middleman
|
||||||
status 404
|
status 404
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -108,11 +136,11 @@ class Middleman::Base
|
||||||
disable :maruku
|
disable :maruku
|
||||||
disable :smush_pngs
|
disable :smush_pngs
|
||||||
disable :automatic_image_sizes
|
disable :automatic_image_sizes
|
||||||
|
disable :relative_assets
|
||||||
|
disable :cache_buster
|
||||||
|
|
||||||
# Default build features
|
# Default build features
|
||||||
configure :build do
|
configure :build do
|
||||||
enable :relative_assets
|
|
||||||
enable :cache_buster
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.new(*args, &bk)
|
def self.new(*args, &bk)
|
||||||
|
|
|
@ -11,7 +11,7 @@ module Middleman
|
||||||
base.set :maruku, {}
|
base.set :maruku, {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_path(path)
|
def render_path(path, layout)
|
||||||
if template_exists?(path, :maruku)
|
if template_exists?(path, :maruku)
|
||||||
render :maruku, path.to_sym
|
render :maruku, path.to_sym
|
||||||
else
|
else
|
||||||
|
|
|
@ -8,11 +8,12 @@ module Middleman
|
||||||
base.helpers Middleman::Haml::Helpers
|
base.helpers Middleman::Haml::Helpers
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_path(path)
|
def render_path(path, layout)
|
||||||
if template_exists?(path, :haml)
|
if template_exists?(path, :haml)
|
||||||
result = nil
|
result = nil
|
||||||
begin
|
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
|
rescue ::Haml::Error => e
|
||||||
result = "Haml Error: #{e}"
|
result = "Haml Error: #{e}"
|
||||||
result << "<pre>Backtrace: #{e.backtrace.join("\n")}</pre>"
|
result << "<pre>Backtrace: #{e.backtrace.join("\n")}</pre>"
|
||||||
|
|
|
@ -8,7 +8,7 @@ module Middleman
|
||||||
base.supported_formats << "sass"
|
base.supported_formats << "sass"
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_path(path)
|
def render_path(path, layout)
|
||||||
if template_exists?(path, :sass)
|
if template_exists?(path, :sass)
|
||||||
begin
|
begin
|
||||||
static_version = options.public + request.path_info
|
static_version = options.public + request.path_info
|
||||||
|
|
1
spec/fixtures/sample/views/custom-layout.html.haml
vendored
Executable file
1
spec/fixtures/sample/views/custom-layout.html.haml
vendored
Executable file
|
@ -0,0 +1 @@
|
||||||
|
%h1 Welcome
|
5
spec/fixtures/sample/views/custom.haml
vendored
Normal file
5
spec/fixtures/sample/views/custom.haml
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
%html
|
||||||
|
%head
|
||||||
|
%title Custom Layout
|
||||||
|
%body
|
||||||
|
= yield
|
22
spec/page_alias_and_layouts_spec.rb
Normal file
22
spec/page_alias_and_layouts_spec.rb
Normal 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
|
Loading…
Reference in a new issue