From 97dc2c9742edf1480a5b97ee21d0356d6770c16f Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Tue, 6 Dec 2011 23:24:14 -0800 Subject: [PATCH] Scenario for using instance vars to pass data to layouts and templates, and prevent changes to instance vars being shared between different templates. --- features/instance_vars.feature | 20 +++++++++++++++++++ fixtures/instance-vars-app/config.rb | 0 .../instance-vars-app/source/_vartial.erb | 5 +++++ .../source/instance-var-set.html.erb | 2 ++ fixtures/instance-vars-app/source/layout.erb | 3 +++ .../source/no-instance-var.html.erb | 1 + lib/middleman/core_extensions/rendering.rb | 16 +++++++++------ 7 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 features/instance_vars.feature create mode 100644 fixtures/instance-vars-app/config.rb create mode 100644 fixtures/instance-vars-app/source/_vartial.erb create mode 100644 fixtures/instance-vars-app/source/instance-var-set.html.erb create mode 100644 fixtures/instance-vars-app/source/layout.erb create mode 100644 fixtures/instance-vars-app/source/no-instance-var.html.erb diff --git a/features/instance_vars.feature b/features/instance_vars.feature new file mode 100644 index 00000000..dd6bcdee --- /dev/null +++ b/features/instance_vars.feature @@ -0,0 +1,20 @@ +Feature: Instance Vars + In order to share data with layouts and partials via instance variables + + Scenario: Setting an instance var in a template should be visible in its layout + Given the Server is running at "instance-vars-app" + When I go to "/instance-var-set.html" + Then I should see "Var is 100" + + Scenario: Setting an instance var in a template should be visible in a partial + Given the Server is running at "instance-vars-app" + When I go to "/instance-var-set.html" + Then I should see "My var is here!" + + Scenario: Setting an instance var in one file should not be visible in another + Given the Server is running at "instance-vars-app" + When I go to "/instance-var-set.html" + When I go to "/no-instance-var.html" + Then I should see "No var..." + + \ No newline at end of file diff --git a/fixtures/instance-vars-app/config.rb b/fixtures/instance-vars-app/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/instance-vars-app/source/_vartial.erb b/fixtures/instance-vars-app/source/_vartial.erb new file mode 100644 index 00000000..cdf13355 --- /dev/null +++ b/fixtures/instance-vars-app/source/_vartial.erb @@ -0,0 +1,5 @@ +<% if @my_var %> + My var is here! +<% else %> + No var... +<% end %> diff --git a/fixtures/instance-vars-app/source/instance-var-set.html.erb b/fixtures/instance-vars-app/source/instance-var-set.html.erb new file mode 100644 index 00000000..d785330f --- /dev/null +++ b/fixtures/instance-vars-app/source/instance-var-set.html.erb @@ -0,0 +1,2 @@ +<% @my_var = 100 %> +<%= partial 'vartial' %> diff --git a/fixtures/instance-vars-app/source/layout.erb b/fixtures/instance-vars-app/source/layout.erb new file mode 100644 index 00000000..cbc7ac71 --- /dev/null +++ b/fixtures/instance-vars-app/source/layout.erb @@ -0,0 +1,3 @@ +Var is <%= @my_var %> + +<%= yield %> \ No newline at end of file diff --git a/fixtures/instance-vars-app/source/no-instance-var.html.erb b/fixtures/instance-vars-app/source/no-instance-var.html.erb new file mode 100644 index 00000000..bcb8a396 --- /dev/null +++ b/fixtures/instance-vars-app/source/no-instance-var.html.erb @@ -0,0 +1 @@ +<%= partial 'vartial' %> diff --git a/lib/middleman/core_extensions/rendering.rb b/lib/middleman/core_extensions/rendering.rb index e5d587df..4de11543 100644 --- a/lib/middleman/core_extensions/rendering.rb +++ b/lib/middleman/core_extensions/rendering.rb @@ -40,8 +40,12 @@ module Middleman::CoreExtensions::Rendering @current_engine, engine_was = engine, @current_engine + # 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 + while ::Tilt[path] - content = render_individual_file(path, locs, opts) + content = render_individual_file(path, locs, opts, context) path = File.basename(path, File.extname(path)) cache.set([:raw_template, path], content) end @@ -49,7 +53,7 @@ module Middleman::CoreExtensions::Rendering needs_layout = !%w(.js .css .txt).include?(extension) if needs_layout && layout_path = fetch_layout(engine, opts) - content = render_individual_file(layout_path, locs, opts) { content } + content = render_individual_file(layout_path, locs, opts, context) { content } end content @@ -92,14 +96,14 @@ module Middleman::CoreExtensions::Rendering end if found_partial - render_individual_file(found_partial, locals, options, &block) + render_individual_file(found_partial, locals, options, self, &block) else raise ::Middleman::CoreExtensions::Rendering::TemplateNotFound, "Could not locate partial: #{data}" end end # @private - def render_individual_file(path, locs = {}, opts = {}, &block) + def render_individual_file(path, locs = {}, opts = {}, context, &block) path = path.to_s @_out_buf, _buf_was = "", @_out_buf @@ -116,7 +120,7 @@ module Middleman::CoreExtensions::Rendering ::Tilt.new(path, 1, options) { body } end - template.render(self, locs, &block) + template.render(context, locs, &block) ensure @_out_buf = _buf_was end @@ -239,4 +243,4 @@ module Middleman::CoreExtensions::Rendering end end end -end \ No newline at end of file +end