From 856f05709c2422d24e99792e3961801d1db29b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Wed, 30 Jul 2014 14:39:24 +0200 Subject: [PATCH] Support template chaining for partials --- .../features/chained_templates.feature | 89 ++++++++++++++++++- .../partial-chained_templates-app/config.rb | 0 .../core_extensions/rendering.rb | 43 ++++++--- 3 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 middleman-core/fixtures/partial-chained_templates-app/config.rb diff --git a/middleman-core/features/chained_templates.feature b/middleman-core/features/chained_templates.feature index b7d4e83e..7f538b65 100644 --- a/middleman-core/features/chained_templates.feature +++ b/middleman-core/features/chained_templates.feature @@ -16,4 +16,91 @@ Feature: Templates should be chainable And the file "index.html" should contain "Title" And the file "index.html" should contain "Subtitle" - And the file "index.html" should contain "Sup" \ No newline at end of file + And the file "index.html" should contain "Sup" + + 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: + """ +

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: + """ +

My Template

+ """ + Then I should see: + """ +

My Partial

+ """ + Then I should see: + """ +

hello world

+ """ + + 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: + """ +

My Template

+ """ + Then I should see: + """ +

My Partial

+ """ + Then I should see: + """ +

hello world

+ """ + + 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: + """ +

My Partial

+ + <%= 'hello world' %> + """ + And the Server is running + When I go to "/my_template.html" + Then I should see: + """ +

My Template

+ """ + Then I should see: + """ +

My Partial

+ """ + Then I should see: + """ +

hello world

+ """ diff --git a/middleman-core/fixtures/partial-chained_templates-app/config.rb b/middleman-core/fixtures/partial-chained_templates-app/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index 074fa017..a4e967d3 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -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