Skip to content

Commit

Permalink
Allow yield() to update options
Browse files Browse the repository at this point in the history
  • Loading branch information
lsegal committed Aug 14, 2009
1 parent 64a1d0d commit 983b8bd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
48 changes: 27 additions & 21 deletions lib/tadpole/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ def init; end
def run(opts = {}, &block)
return '' if run_before_run.is_a?(FalseClass)

old_opts = options
self.options = options.to_hash.update(opts)
out = run_sections(@compiled_sections || sections, &block)
self.options = old_opts
out
run_sections(@compiled_sections || sections, false, opts, &block)
rescue => e
begin
provider = find_section_provider(current_section)
Expand All @@ -139,27 +135,29 @@ def run(opts = {}, &block)
alias to_s run

def run_sections(sects, break_first = false, locals = {}, &block)
out = ''
raise ArgumentError, "Template(#{path}) is missing sections" unless sects
sects = sects.first if sects.first.is_a?(Array)
sects.each_with_index do |section, i|
(break_first ? break : next) if section.is_a?(Array)
with_locals(locals) do
out = ''
raise ArgumentError, "Template(#{path}) is missing sections" unless sects
sects = sects.first if sects.first.is_a?(Array)
sects.each_with_index do |section, i|
(break_first ? break : next) if section.is_a?(Array)

self.current_section = section_name(section)
self.current_section = section_name(section)

next if run_before_sections.is_a?(FalseClass)
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
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

break if break_first
break if break_first
end
out
end
out
end

def run_subsections(section, subsections, locals = {}, &block)
Expand Down Expand Up @@ -238,6 +236,14 @@ def parse_yield_args(*args)
[nil, {}]
end
end

def with_locals(locals = {}, &block)
old_options = options
self.options = options.to_hash.merge(locals)
result = yield
self.options = old_options
result
end

def find_section_provider(section)
section = section.to_s
Expand Down
21 changes: 21 additions & 0 deletions spec/examples/yield/setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def init
sections :a, [:b, [:c, :d]]
end

def a
'[' + yield(:foo => 'FOO') + ']'
end

def b
foo + yieldall(:bar => 'BAR')
end

def c
bar
end

def d
bar
end

# expected [FOOBARBAR]
12 changes: 12 additions & 0 deletions spec/yield_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require File.join(File.dirname(__FILE__), '..', 'lib', 'tadpole')

describe Tadpole, '::Template' do
before do
Tadpole.template_paths.clear
Tadpole.register_template_path File.dirname(__FILE__) + '/examples'
end

it "should yield with extra values" do
Template('yield').new.run.should == '[FOOBARBAR]'
end
end

0 comments on commit 983b8bd

Please sign in to comment.