diff --git a/middleman-core/features/instance_vars.feature b/middleman-core/features/instance_vars.feature new file mode 100644 index 00000000..ef240c57 --- /dev/null +++ b/middleman-core/features/instance_vars.feature @@ -0,0 +1,9 @@ +Feature: Instance Variables + Scenario: A dynamic page template using instance variables + Given the Server is running at "instance-vars-app" + When I go to "/a.html" + Then I should see "A: 'set'" + Then I should see "B: ''" + When I go to "/b.html" + Then I should see "A: ''" + Then I should see "B: 'set'" diff --git a/middleman-core/fixtures/instance-vars-app/config.rb b/middleman-core/fixtures/instance-vars-app/config.rb new file mode 100644 index 00000000..76367d3f --- /dev/null +++ b/middleman-core/fixtures/instance-vars-app/config.rb @@ -0,0 +1,7 @@ +page "a.html", :proxy => 'content.html', :ignore => true do + @a = "set" +end + +page "b.html", :proxy => 'content.html', :ignore => true do + @b = "set" +end diff --git a/middleman-core/fixtures/instance-vars-app/source/content.html.erb b/middleman-core/fixtures/instance-vars-app/source/content.html.erb new file mode 100644 index 00000000..58b6399d --- /dev/null +++ b/middleman-core/fixtures/instance-vars-app/source/content.html.erb @@ -0,0 +1,2 @@ +A: '<%= @a %>' +B: '<%= @b %>' diff --git a/middleman-core/fixtures/instance-vars-app/source/layout.erb b/middleman-core/fixtures/instance-vars-app/source/layout.erb new file mode 100644 index 00000000..cd9bb66d --- /dev/null +++ b/middleman-core/fixtures/instance-vars-app/source/layout.erb @@ -0,0 +1 @@ +<%= yield %> \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index 4e08ad8b..98fc1fb1 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -96,7 +96,7 @@ module Middleman # @param [Hash] locs # @param [Hash] opts # @return [String] - def render_template(path, locs={}, opts={}) + def render_template(path, locs={}, opts={}, blocks=[]) # Detect the remdering engine from the extension extension = File.extname(path) engine = extension[1..-1].to_sym @@ -107,6 +107,9 @@ module Middleman # Use a dup of self as a context so that instance variables set within # the template don't persist for other templates. context = self.dup + blocks.each do |block| + context.instance_eval(&block) + end # Store current locs/opts for later @current_locs = locs, @current_opts = opts diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 8d70c514..c7236c74 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -125,14 +125,11 @@ module Middleman app.data.store("page", md[:page]) end - md[:blocks].each do |aBlock| - app.instance_eval(&aBlock) - end - - app.instance_eval(&block) if block_given? - + blocks = md[:blocks].dup rescue [] + blocks << block if block_given? + app.current_path ||= self.destination_path - app.render_template(source_file, locs, opts) + app.render_template(source_file, locs, opts, blocks) end end