page routing is a core ext
This commit is contained in:
parent
118d23be41
commit
efe7332617
|
@ -91,6 +91,9 @@ module Middleman
|
||||||
|
|
||||||
# Extended version of Padrino's rendering
|
# Extended version of Padrino's rendering
|
||||||
autoload :Rendering, "middleman/core_extensions/rendering"
|
autoload :Rendering, "middleman/core_extensions/rendering"
|
||||||
|
|
||||||
|
# Pass custom options to views
|
||||||
|
autoload :Routing, "middleman/core_extensions/routing"
|
||||||
end
|
end
|
||||||
|
|
||||||
module Features
|
module Features
|
||||||
|
|
49
lib/middleman/core_extensions/routing.rb
Normal file
49
lib/middleman/core_extensions/routing.rb
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
module Middleman::CoreExtensions::Routing
|
||||||
|
class << self
|
||||||
|
def registered(app)
|
||||||
|
app.extend ClassMethods
|
||||||
|
end
|
||||||
|
alias :included :registered
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def current_layout
|
||||||
|
@layout
|
||||||
|
end
|
||||||
|
|
||||||
|
# Takes a block which allows many pages to have the same layout
|
||||||
|
# with_layout :admin do
|
||||||
|
# page "/admin/"
|
||||||
|
# page "/admin/login.html"
|
||||||
|
# end
|
||||||
|
def with_layout(layout_name, &block)
|
||||||
|
old_layout = current_layout
|
||||||
|
|
||||||
|
layout(layout_name)
|
||||||
|
class_eval(&block) if block_given?
|
||||||
|
ensure
|
||||||
|
layout(old_layout)
|
||||||
|
end
|
||||||
|
|
||||||
|
# The page method allows the layout to be set on a specific path
|
||||||
|
# page "/about.html", :layout => false
|
||||||
|
# page "/", :layout => :homepage_layout
|
||||||
|
def page(url, options={}, &block)
|
||||||
|
url = url.gsub(%r{#{settings.index_file}$}, "")
|
||||||
|
url = url.gsub(%r{(\/)$}, "") if url.length > 1
|
||||||
|
|
||||||
|
paths = [url]
|
||||||
|
paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1
|
||||||
|
paths << "/#{path_to_index(url)}"
|
||||||
|
|
||||||
|
options[:layout] = current_layout if options[:layout].nil?
|
||||||
|
|
||||||
|
paths.each do |p|
|
||||||
|
get(p) do
|
||||||
|
return yield if block_given?
|
||||||
|
process_request(options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -6,7 +6,6 @@ module Middleman
|
||||||
# Basic Sinatra config
|
# Basic Sinatra config
|
||||||
set :app_file, __FILE__
|
set :app_file, __FILE__
|
||||||
set :root, ENV["MM_DIR"] || Dir.pwd
|
set :root, ENV["MM_DIR"] || Dir.pwd
|
||||||
set :reload, false
|
|
||||||
set :sessions, false
|
set :sessions, false
|
||||||
set :logging, false
|
set :logging, false
|
||||||
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
|
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
|
||||||
|
@ -48,6 +47,9 @@ module Middleman
|
||||||
# Parse YAML from templates
|
# Parse YAML from templates
|
||||||
register Middleman::CoreExtensions::FrontMatter
|
register Middleman::CoreExtensions::FrontMatter
|
||||||
|
|
||||||
|
# with_layout and page routing
|
||||||
|
register Middleman::CoreExtensions::Routing
|
||||||
|
|
||||||
set :default_extensions, [
|
set :default_extensions, [
|
||||||
:lorem
|
:lorem
|
||||||
]
|
]
|
||||||
|
@ -62,54 +64,9 @@ module Middleman
|
||||||
super(option, value, &nil)
|
super(option, value, &nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Rack helper for adding mime-types during local preview
|
|
||||||
def self.mime(ext, type)
|
|
||||||
ext = ".#{ext}" unless ext.to_s[0] == ?.
|
|
||||||
::Rack::Mime::MIME_TYPES[ext.to_s] = type
|
|
||||||
end
|
|
||||||
|
|
||||||
# Default layout name
|
# Default layout name
|
||||||
layout :layout
|
layout :layout
|
||||||
|
|
||||||
def self.current_layout
|
|
||||||
@layout
|
|
||||||
end
|
|
||||||
|
|
||||||
# Takes a block which allows many pages to have the same layout
|
|
||||||
# with_layout :admin do
|
|
||||||
# page "/admin/"
|
|
||||||
# page "/admin/login.html"
|
|
||||||
# end
|
|
||||||
def self.with_layout(layout_name, &block)
|
|
||||||
old_layout = current_layout
|
|
||||||
|
|
||||||
layout(layout_name)
|
|
||||||
class_eval(&block) if block_given?
|
|
||||||
ensure
|
|
||||||
layout(old_layout)
|
|
||||||
end
|
|
||||||
|
|
||||||
# The page method allows the layout to be set on a specific path
|
|
||||||
# page "/about.html", :layout => false
|
|
||||||
# page "/", :layout => :homepage_layout
|
|
||||||
def self.page(url, options={}, &block)
|
|
||||||
url = url.gsub(%r{#{settings.index_file}$}, "")
|
|
||||||
url = url.gsub(%r{(\/)$}, "") if url.length > 1
|
|
||||||
|
|
||||||
paths = [url]
|
|
||||||
paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1
|
|
||||||
paths << "/#{path_to_index(url)}"
|
|
||||||
|
|
||||||
options[:layout] = current_layout if options[:layout].nil?
|
|
||||||
|
|
||||||
paths.each do |p|
|
|
||||||
get(p) do
|
|
||||||
return yield if block_given?
|
|
||||||
process_request(options)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# This will match all requests not overridden in the project's config.rb
|
# This will match all requests not overridden in the project's config.rb
|
||||||
not_found do
|
not_found do
|
||||||
process_request
|
process_request
|
||||||
|
|
Loading…
Reference in a new issue