move current_path in to the template context, one less piece of global state
This commit is contained in:
parent
f40903e663
commit
614d69dc18
|
@ -153,20 +153,6 @@ module Middleman
|
||||||
# Methods to be mixed-in to Middleman::Application
|
# Methods to be mixed-in to Middleman::Application
|
||||||
module InstanceMethods
|
module InstanceMethods
|
||||||
|
|
||||||
# Accessor for current path
|
|
||||||
# @return [String]
|
|
||||||
def current_path
|
|
||||||
Thread.current[:current_path]
|
|
||||||
end
|
|
||||||
|
|
||||||
# Set the current path
|
|
||||||
#
|
|
||||||
# @param [String] path The new current path
|
|
||||||
# @return [void]
|
|
||||||
def current_path=(path)
|
|
||||||
Thread.current[:current_path] = path
|
|
||||||
end
|
|
||||||
|
|
||||||
delegate :use, :to => :"self.class"
|
delegate :use, :to => :"self.class"
|
||||||
delegate :map, :to => :"self.class"
|
delegate :map, :to => :"self.class"
|
||||||
|
|
||||||
|
@ -210,7 +196,6 @@ module Middleman
|
||||||
# @param [Rack::Response] res
|
# @param [Rack::Response] res
|
||||||
def process_request(env, req, res)
|
def process_request(env, req, res)
|
||||||
start_time = Time.now
|
start_time = Time.now
|
||||||
current_path = nil
|
|
||||||
|
|
||||||
request_path = URI.decode(env['PATH_INFO'].dup)
|
request_path = URI.decode(env['PATH_INFO'].dup)
|
||||||
if request_path.respond_to? :force_encoding
|
if request_path.respond_to? :force_encoding
|
||||||
|
@ -230,17 +215,12 @@ module Middleman
|
||||||
# If this path is a binary file, send it immediately
|
# If this path is a binary file, send it immediately
|
||||||
return send_file(resource, env) if resource.binary?
|
return send_file(resource, env) if resource.binary?
|
||||||
|
|
||||||
current_path = resource.destination_path
|
|
||||||
|
|
||||||
res['Content-Type'] = resource.content_type || 'text/plain'
|
res['Content-Type'] = resource.content_type || 'text/plain'
|
||||||
|
|
||||||
begin
|
begin
|
||||||
# Write out the contents of the page
|
# Write out the contents of the page
|
||||||
output = resource.render do
|
res.write resource.render
|
||||||
self.current_path = current_path
|
|
||||||
end
|
|
||||||
|
|
||||||
res.write output
|
|
||||||
# Valid content is a 200 status
|
# Valid content is a 200 status
|
||||||
res.status = 200
|
res.status = 200
|
||||||
rescue Middleman::TemplateRenderer::TemplateNotFound => e
|
rescue Middleman::TemplateRenderer::TemplateNotFound => e
|
||||||
|
@ -249,7 +229,7 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
# End the request
|
# End the request
|
||||||
logger.debug "== Finishing Request: #{current_path} (#{(Time.now - start_time).round(2)}s)"
|
logger.debug "== Finishing Request: #{resource.destination_path} (#{(Time.now - start_time).round(2)}s)"
|
||||||
halt res.finish
|
halt res.finish
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,9 @@ module Middleman
|
||||||
|
|
||||||
# Sitemap instance methods
|
# Sitemap instance methods
|
||||||
module InstanceMethods
|
module InstanceMethods
|
||||||
|
def current_path
|
||||||
|
@current_locs[:current_path]
|
||||||
|
end
|
||||||
|
|
||||||
# Get the resource object for the current path
|
# Get the resource object for the current path
|
||||||
# @return [Middleman::Sitemap::Resource]
|
# @return [Middleman::Sitemap::Resource]
|
||||||
|
|
|
@ -135,7 +135,7 @@ module Middleman
|
||||||
blocks = Array(md[:blocks]).dup
|
blocks = Array(md[:blocks]).dup
|
||||||
blocks << block if block_given?
|
blocks << block if block_given?
|
||||||
|
|
||||||
app.current_path ||= self.destination_path
|
locs[:current_path] ||= self.destination_path
|
||||||
|
|
||||||
# Certain output file types don't use layouts
|
# Certain output file types don't use layouts
|
||||||
if !opts.has_key?(:layout)
|
if !opts.has_key?(:layout)
|
||||||
|
|
|
@ -4,7 +4,7 @@ require 'middleman-core/template_renderer'
|
||||||
module Middleman
|
module Middleman
|
||||||
class TemplateContext
|
class TemplateContext
|
||||||
attr_reader :app
|
attr_reader :app
|
||||||
attr_accessor :current_engine, :current_path
|
attr_accessor :current_engine
|
||||||
|
|
||||||
delegate :config, :logger, :sitemap, :build?, :development?, :data, :extensions, :source_dir, :root, :to => :app
|
delegate :config, :logger, :sitemap, :build?, :development?, :data, :extensions, :source_dir, :root, :to => :app
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue