tests for partials

This commit is contained in:
Thomas Reynolds 2011-11-26 15:32:41 -08:00
parent 2b545f8e35
commit 7c50c9e4d5
13 changed files with 84 additions and 31 deletions

View file

@ -381,16 +381,40 @@ class Middleman::Base
# @return [String] Output
def render(engine, data, options={}, locals={}, &block)
data = data.to_s
template_path = File.join(source_dir, data)
if sitemap.exists?(data)
sitemap.page(data).render(options, locals, &block)
elsif File.exists?(template_path)
body = app.cache.fetch(:raw_template, template_path) do
File.read(template_path)
end
found_partial = false
engine = nil
if sitemap.exists?(current_path)
page = sitemap.page(current_path)
current_dir = File.dirname(page.source_file)
engine = File.extname(page.source_file)[1..-1].to_sym
Middleman::Sitemap::Template.static_render(self, template_path, body, locals, options, &block)
if current_dir != self.source_dir
relative_dir = File.join(current_dir.sub("#{self.source_dir}/", ""), data)
found_partial, found_engine = Middleman::Sitemap::Template.resolve_template(self, relative_dir, :preferred_engine => engine)
if !found_partial
found_partial, found_engine = Middleman::Sitemap::Template.resolve_template(self, relative_dir)
end
end
end
if !found_partial && !engine.nil?
found_partial, found_engine = Middleman::Sitemap::Template.resolve_template(self, data, :preferred_engine => engine)
end
if !found_partial
found_partial, found_engine = Middleman::Sitemap::Template.resolve_template(self, data)
end
if found_partial
body = cache.fetch(:raw_template, found_partial) do
File.read(found_partial)
end
Middleman::Sitemap::Template.static_render(self, found_partial, body, locals, options, &block)
else
throw "Could not find file to render: #{data}"
end

View file

@ -124,28 +124,28 @@ module Middleman::Sitemap
if !preferred_engine.nil?
# Check root
layout_path, *etc = resolve_template(name, :preferred_engine => preferred_engine)
layout_path, *etc = self.class.resolve_template(app, name, :preferred_engine => preferred_engine)
# Check layouts folder
if !layout_path
layout_path, *etc = resolve_template(File.join("layouts", name.to_s), :preferred_engine => preferred_engine)
layout_path, *etc = self.class.resolve_template(app, File.join("layouts", name.to_s), :preferred_engine => preferred_engine)
end
end
# Check root, no preference
if !layout_path
layout_path, *etc = resolve_template(name)
layout_path, *etc = self.class.resolve_template(app, name)
end
# Check layouts folder, no preference
if !layout_path
layout_path, *etc = resolve_template(File.join("layouts", name.to_s))
layout_path, *etc = self.class.resolve_template(app, File.join("layouts", name.to_s))
end
layout_path
end
def resolve_template(request_path, options={})
def self.resolve_template(app, request_path, options={})
request_path = request_path.to_s
cache.fetch(:resolve_template, request_path, options) do
relative_path = request_path.sub(%r{^/}, "")
@ -196,9 +196,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
template = ::Tilt.new(path, 1, options) { body }
# end
template = cache.fetch(:compiled_template, options, body) do
::Tilt.new(path, 1, options) { body }
end
template.render(app, locs, &block)
end