Merge pull request #550 from rumpuslabs/instance_vars_fix
Fix instance variables in dynamic pages
This commit is contained in:
commit
4410d858f2
6 changed files with 27 additions and 8 deletions
9
middleman-core/features/instance_vars.feature
Normal file
9
middleman-core/features/instance_vars.feature
Normal file
|
@ -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'"
|
7
middleman-core/fixtures/instance-vars-app/config.rb
Normal file
7
middleman-core/fixtures/instance-vars-app/config.rb
Normal file
|
@ -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
|
|
@ -0,0 +1,2 @@
|
|||
A: '<%= @a %>'
|
||||
B: '<%= @b %>'
|
|
@ -0,0 +1 @@
|
|||
<%= yield %>
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue