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

30
features/partials.feature Normal file
View file

@ -0,0 +1,30 @@
Feature: Provide Sane Defaults for Partial Behavior
Scenario: Finds shared partials relative to the root
Given the Server is running at "partials-app"
When I go to "/index.html"
Then I should see "Header"
And I should see "Footer"
Scenario: Finds shared partials relative to the root (sub)
Given the Server is running at "partials-app"
When I go to "/sub/index.html"
Then I should see "Header"
And I should see "Footer"
Scenario: Prefers partials of the same engine type
Given the Server is running at "partials-app"
When I go to "/index.html"
Then I should see "ERb Main"
Scenario: Prefers partials of the same engine type
Given the Server is running at "partials-app"
When I go to "/second.html"
Then I should see "Haml Main"
And I should see "Header"
And I should see "Footer"
Scenario: Finds partial relative to template
Given the Server is running at "partials-app"
When I go to "/sub/index.html"
Then I should see "Local Partial"

View file

@ -1,15 +0,0 @@
@wip
Feature: Tiny Src
In order automatically scale images for mobile devices
Scenario: Rendering html with the feature disabled
Given "tiny_src" feature is "disabled"
And the Server is running at "test-app"
When I go to "/tiny_src.html"
Then I should see "http://test.com/image.jpg"
Scenario: Rendering html with the feature enabled
Given "tiny_src" feature is "enabled"
And the Server is running at "test-app"
When I go to "/tiny_src.html"
Then I should see "http://i.tinysrc.mobi/http://test.com/image.jpg"

View file

View file

@ -0,0 +1 @@
ERb Main

View file

@ -0,0 +1 @@
Haml Main

View file

@ -0,0 +1,3 @@
<%= partial "shared/header" %>
<%= partial "main" %>
<%= partial "shared/footer" %>

View file

@ -0,0 +1,3 @@
= partial "shared/header"
= partial "main"
= partial "shared/footer"

View file

@ -0,0 +1 @@
%footer Footer

View file

@ -0,0 +1 @@
<header>Header</header>

View file

@ -0,0 +1 @@
<h2>Local Partial</h2>

View file

@ -0,0 +1,3 @@
<%= partial "shared/header" %>
<%= partial "local" %>
<%= partial "shared/footer" %>

View file

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

View file

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