Merge pull request #854 from artm/partials_dir

A setting for custom partials dir
This commit is contained in:
Artem Baguinski 2013-04-08 23:47:58 -07:00
commit fe6a18fbe6
8 changed files with 51 additions and 11 deletions

View file

@ -87,6 +87,10 @@ module Middleman
# @return [String] # @return [String]
config.define_setting :fonts_dir, "fonts", 'Location of fonts within source' config.define_setting :fonts_dir, "fonts", 'Location of fonts within source'
# Location of partials within source. Used by renderers.
# @return [String]
config.define_setting :partials_dir, "", 'Location of partials within source'
# Where to build output files # Where to build output files
# @return [String] # @return [String]
config.define_setting :build_dir, "build", 'Where to build output files' config.define_setting :build_dir, "build", 'Where to build output files'

View file

@ -196,8 +196,7 @@ module Middleman
engine = File.extname(resource.source_file)[1..-1].to_sym engine = File.extname(resource.source_file)[1..-1].to_sym
# Look for partials relative to the current path # Look for partials relative to the current path
if current_dir != self.source_dir relative_dir = File.join(current_dir.sub(%r{^#{self.source_dir}/?}, ""), data)
relative_dir = File.join(current_dir.sub("#{self.source_dir}/", ""), data)
# Try to use the current engine first # Try to use the current engine first
found_partial, found_engine = resolve_template(relative_dir, :preferred_engine => engine, :try_without_underscore => true) found_partial, found_engine = resolve_template(relative_dir, :preferred_engine => engine, :try_without_underscore => true)
@ -207,16 +206,16 @@ module Middleman
found_partial, found_engine = resolve_template(relative_dir, :try_without_underscore => true) found_partial, found_engine = resolve_template(relative_dir, :try_without_underscore => true)
end end
end end
end
# Look in the root for the partial with the current engine # Look in the partials_dir for the partial with the current engine
partials_path = File.join(config[:partials_dir], data)
if !found_partial && !engine.nil? if !found_partial && !engine.nil?
found_partial, found_engine = resolve_template(data, :preferred_engine => engine, :try_without_underscore => true) found_partial, found_engine = resolve_template(partials_path, :preferred_engine => engine, :try_without_underscore => true)
end end
# Look in the root with any engine # Look in the root with any engine
if !found_partial if !found_partial
found_partial, found_engine = resolve_template(data, :try_without_underscore => true) found_partial, found_engine = resolve_template(partials_path, :try_without_underscore => true)
end end
# Render the partial if found, otherwide throw exception # Render the partial if found, otherwide throw exception

View file

@ -0,0 +1,30 @@
Feature: Partials dir
Scenario: Find partials in a custom partials dir
Given a fixture app "partials-dir-app"
And a file named "config.rb" with:
"""
set :partials_dir, 'partials'
"""
And the Server is running
When I go to "/index.html"
Then I should see "contents of the partial"
Scenario: Find partials in a nested custom partials dir
Given a fixture app "partials-dir-app"
And a file named "config.rb" with:
"""
set :partials_dir, 'nested/partials'
"""
And the Server is running
When I go to "/index.html"
Then I should see "contents of the nested partial"
Scenario: Find partials in the default partials dir
Given a fixture app "default-partials-dir-app"
And a file named "config.rb" with:
"""
"""
And the Server is running
When I go to "/index.html"
Then I should see "contents of the partial"

View file

@ -0,0 +1 @@
contents of the partial

View file

@ -0,0 +1,2 @@
<%= partial 'partial' %>

View file

@ -0,0 +1,2 @@
<%= partial 'partial' %>

View file

@ -0,0 +1 @@
contents of the nested partial

View file

@ -0,0 +1 @@
contents of the partial