Skip to content

Commit

Permalink
Fix yield rendering behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
lsegal committed Jun 15, 2008
1 parent 8f9af30 commit e3da208
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/tadpole/main.rb
Expand Up @@ -6,6 +6,7 @@ class Insertion
def initialize(list, value) @list, @value = list, value end
def before(val) insertion(val, 0) end
def after(val) insertion(val, 1) end
def at(val) insertion(val, 0); @list.delete(val); @list end
private
def insertion(val, rel)
if index = @list.index(val)
Expand Down
9 changes: 6 additions & 3 deletions lib/tadpole/template.rb
Expand Up @@ -132,7 +132,9 @@ def run_sections(sects, break_first = false, locals = {}, &block)
next if run_before_sections.is_a?(FalseClass)

if sects[i+1].is_a?(Array)
old, self.subsections = subsections, sects[i+1]
out += run_subsections(section, sects[i+1], locals, &block)
self.subsections = old
else
out += render(section, locals, true, &block)
end
Expand All @@ -143,7 +145,7 @@ def run_sections(sects, break_first = false, locals = {}, &block)
end

def run_subsections(section, subsections, locals = {}, &block)
self.subsections = subsections.reject {|s| Array === s }
p subsections
list = subsections.dup

render(section, locals, true) do |*args|
Expand All @@ -165,12 +167,13 @@ def run_subsections(section, subsections, locals = {}, &block)

def all_sections(&block)
subsections.each do |s|
next if Array === s
yield section_name(s)
end
end

def yieldall(locals = {}, &block)
subsections.map {|s| render(s, locals, &block) }.join
run_sections(subsections, false, locals, &block)
end

def render(section, locals = {}, call_method = false, &block)
Expand Down Expand Up @@ -205,7 +208,7 @@ def section_name(section)
@providers.index(section) || section
else
section.respond_to?(:path) ? section.path : section
end
end.to_s
end

def parse_yield_args(*args)
Expand Down
12 changes: 12 additions & 0 deletions spec/examples/render/6/setup.rb
@@ -0,0 +1,12 @@
def init
sections [:a, [:b, [:c, :d], :e, :f]]
end

def a; 'A' + yieldall + 'G' end
def b; 'B(' + subsections.map {|x| section_name(x).upcase }.join + ')' end
def c; 'XXX' end
def d; 'XXX' end
def e; 'E' end
def f; 'F' end

# Expected: AB(CD)EFG
4 changes: 4 additions & 0 deletions spec/render_spec.rb
Expand Up @@ -25,4 +25,8 @@
it "should restart subsection render loop if yield is called more times than subsections" do
Template('render/5').new.run.should == 'xyzy'
end

it "should handle subsections with subsections" do
Template('render/6').new.run.should == 'AB(CD)EFG'
end
end

0 comments on commit e3da208

Please sign in to comment.