Support template chaining for partials

remove_hooks
Dennis Günnewig 2014-07-30 14:39:24 +02:00
parent 0add2e6676
commit 856f05709c
3 changed files with 118 additions and 14 deletions

View File

@ -16,4 +16,91 @@ Feature: Templates should be chainable
And the file "index.html" should contain "Title</h1>"
And the file "index.html" should contain "Subtitle</h2>"
And the file "index.html" should contain "Sup</h3>"
And the file "index.html" should contain "Sup</h3>"
Scenario: Partials are parsed by multiple template engines: Outer template has .erb and inner .md.erb
Given a fixture app "partial-chained_templates-app"
And a template named "my_template.html.erb" with:
"""
<h1>My Template</h1>
<%= partial 'my_partial' %>
"""
And a template named "my_partial.html.md.erb" with:
"""
## My Partial
<%= 'hello world' %>
"""
And the Server is running
When I go to "/my_template.html"
Then I should see:
"""
<h1>My Template</h1>
"""
Then I should see:
"""
<h2 id="my-partial">My Partial</h2>
"""
Then I should see:
"""
<p>hello world</p>
"""
Scenario: Partials are parsed by multiple template engines: Outer template has .md.erb and inner .md.erb
Given a fixture app "partial-chained_templates-app"
And a template named "my_template.html.md.erb" with:
"""
# My Template
<%= partial 'my_partial' %>
"""
And a template named "my_partial.html.md.erb" with:
"""
## My Partial
<%= 'hello world' %>
"""
And the Server is running
When I go to "/my_template.html"
Then I should see:
"""
<h1 id="my-template">My Template</h1>
"""
Then I should see:
"""
<h2 id="my-partial">My Partial</h2>
"""
Then I should see:
"""
<p>hello world</p>
"""
Scenario: Partials are parsed by multiple template engines: Outer template has .md.erb, and inner .erb
Given a fixture app "partial-chained_templates-app"
And a template named "my_template.html.md.erb" with:
"""
# My Template
<%= partial 'my_partial' %>
"""
And a template named "my_partial.html.erb" with:
"""
<h2>My Partial</h2>
<%= 'hello world' %>
"""
And the Server is running
When I go to "/my_template.html"
Then I should see:
"""
<h1 id="my-template">My Template</h1>
"""
Then I should see:
"""
<h2>My Partial</h2>
"""
Then I should see:
"""
<p>hello world</p>
"""

View File

@ -146,18 +146,7 @@ module Middleman
@current_locs = locs
@current_opts = opts
# Keep rendering template until we've used up all extensions. This
# handles cases like `style.css.sass.erb`
content = nil
while ::Tilt[path]
begin
opts[:template_body] = content if content
content = render_individual_file(path, locs, opts, context)
path = File.basename(path, File.extname(path))
rescue LocalJumpError
raise "Tried to render a layout (calls yield) at #{path} like it was a template. Non-default layouts need to be in #{source}/#{config[:layouts_dir]}."
end
end
content = _render_with_all_renderers(path, locs, context, opts)
# If we need a layout and have a layout, use it
if layout_path = fetch_layout(engine, opts)
@ -175,6 +164,34 @@ module Middleman
@current_opts = nil
end
private
def _render_with_all_renderers(path, locs, context, opts, &block)
# Keep rendering template until we've used up all extensions. This
# handles cases like `style.css.sass.erb`
content = nil
while ::Tilt[path]
begin
opts[:template_body] = content if content
content = if block_given?
render_individual_file(path, locs, opts, context, &block)
else
render_individual_file(path, locs, opts, context)
end
path = File.basename(path, File.extname(path))
rescue LocalJumpError
raise "Tried to render a layout (calls yield) at #{path} like it was a template. Non-default layouts need to be in #{source}/#{config[:layouts_dir]}."
end
end
content
end
public
# Sinatra/Padrino compatible render method signature referenced by some view
# helpers. Especially partials.
#
@ -215,7 +232,7 @@ module Middleman
File.read(r.source_file)
else
# Render the partial if found, otherwide throw exception
render_individual_file(found_partial, locals, options, self, &block)
_render_with_all_renderers(found_partial, locals, self, options, &block)
end
end