Fix instance variables in dynamic pages

This commit is contained in:
Tim Bates 2012-07-23 15:15:10 +09:30
parent 2f7371ca1f
commit 99a8723ea6
6 changed files with 27 additions and 8 deletions

View 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'"

View 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

View file

@ -0,0 +1,2 @@
A: '<%= @a %>'
B: '<%= @b %>'

View file

@ -0,0 +1 @@
<%= yield %>

View file

@ -96,7 +96,7 @@ module Middleman
# @param [Hash] locs # @param [Hash] locs
# @param [Hash] opts # @param [Hash] opts
# @return [String] # @return [String]
def render_template(path, locs={}, opts={}) def render_template(path, locs={}, opts={}, blocks=[])
# Detect the remdering engine from the extension # Detect the remdering engine from the extension
extension = File.extname(path) extension = File.extname(path)
engine = extension[1..-1].to_sym 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 # Use a dup of self as a context so that instance variables set within
# the template don't persist for other templates. # the template don't persist for other templates.
context = self.dup context = self.dup
blocks.each do |block|
context.instance_eval(&block)
end
# Store current locs/opts for later # Store current locs/opts for later
@current_locs = locs, @current_opts = opts @current_locs = locs, @current_opts = opts

View file

@ -125,14 +125,11 @@ module Middleman
app.data.store("page", md[:page]) app.data.store("page", md[:page])
end end
md[:blocks].each do |aBlock| blocks = md[:blocks].dup rescue []
app.instance_eval(&aBlock) blocks << block if block_given?
end
app.instance_eval(&block) if block_given?
app.current_path ||= self.destination_path app.current_path ||= self.destination_path
app.render_template(source_file, locs, opts) app.render_template(source_file, locs, opts, blocks)
end end
end end