Permalink
Browse files

Allow multiple scope arguments in Template#render and Dialect.render

  • Loading branch information...
1 parent fb08b7b commit f5ec0c59757f2f9b77e0ad49f11a17572c9b04b3 @blambeau committed Jun 13, 2012
View
@@ -14,8 +14,8 @@ def compile(source, options = {})
Template.new source, :dialect => self
end
- def render(source, scope = {}, buffer = "")
- compile(source).call(scope, buffer)
+ def render(source, *args)
+ compile(source).call(*args)
end
# tag installation and dispatching
View
@@ -44,8 +44,8 @@ def to_ast
compiler.to_ast(template_content)
end
- def call(locals = {}, buffer = '')
- scope = WLang::Scope.chain([self.locals, locals])
+ def call(*args)
+ scope, buffer = call_args_conventions(args)
dialect_instance.dup.render compiled, scope, buffer
end
alias :render :call
@@ -59,5 +59,13 @@ def yaml_front_matter?
opt.nil? or opt
end
+ def call_args_conventions(args)
+ args << '' unless args.last.respond_to?(:<<)
+ buffer = args.pop
+ args << self.locals unless self.locals.empty?
+ scope = WLang::Scope.chain(args)
+ [scope, buffer]
+ end
+
end # class Template
end # module WLang
@@ -35,7 +35,7 @@ def prepare
def evaluate(scope, locals, &block)
locals[:yield] = block if block
- @engine.render WLang::Scope.chain([scope, locals])
+ @engine.render(scope, locals)
end
end
@@ -1,4 +0,0 @@
----
-who: world
----
-Hello ${who}!
@@ -1,16 +0,0 @@
-require 'spec_helper'
-module WLang
- describe "WLang scoping rules" do
-
- let(:template){ Template.new(Path.dir / 'hello.wlang') }
-
- specify 'template locals are used by default' do
- template.render.should eq("Hello world!")
- end
-
- specify 'template locals are overriden by render locals' do
- template.render(:who => "wlang").should eq("Hello wlang!")
- end
-
- end
-end
@@ -17,6 +17,10 @@ module WLang
U.render(hello_tpl, {}).should eq(expected)
end
+ it "accepts multiple scope objects" do
+ U.render(hello_tpl, 12, {}).should eq(expected)
+ end
+
it 'accepts a :to_path object' do
U.render(hello_path).should eq(expected)
end
@@ -9,6 +9,11 @@ module WLang
let(:scope) { subject.first }
let(:buffer){ subject.last }
+ before do
+ scope.should be_a(Scope)
+ buffer.should respond_to(:<<)
+ end
+
context 'without any argument' do
let(:args){ [] }
it 'sets a default NullScope' do
@@ -20,13 +25,12 @@ module WLang
end
context 'with only a String buffer' do
- let(:args){ [ '' ] }
+ let(:args){ [ 'foo' ] }
it 'sets a default NullScope' do
scope.should eq(Scope.null)
end
it 'uses the provided buffer' do
- buffer.should eq(args.first)
- buffer.object_id.should eq(args.first.object_id)
+ buffer.should eq('foo')
end
end
@@ -43,9 +47,8 @@ module WLang
context 'with only a Hash' do
let(:args){ [ {} ] }
- it 'sets a default ObjectScope' do
- scope.should be_a(Scope::ObjectScope)
- scope.subject.should eq(args.first)
+ it 'sets the correct scope subjects' do
+ scope.subjects.should eq(args)
end
it 'uses a string buffer' do
buffer.should eq('')
@@ -55,34 +58,42 @@ module WLang
context 'with multiple scoping arguments' do
let(:args){ [ 12, {} ] }
it 'builds a chain of scoping arguments' do
- scope.should be_a(Scope::ObjectScope)
- scope.subject.should eq(args.last)
- scope.parent.subject.should eq(args.first)
- scope.root.subject.should eq(args.first)
+ scope.subjects.should eq(args)
end
it 'uses a string buffer' do
buffer.should eq('')
end
end
context 'with both a Hash and a String' do
- let(:args){ [ {}, '' ] }
+ let(:args){ [ {}, 'foo' ] }
it 'sets a default ObjectScope' do
- scope.should be_a(Scope::ObjectScope)
- scope.subject.should eq(args.first)
+ scope.subjects.should eq([args.first])
end
it 'uses a string buffer' do
- buffer.should eq(args.last)
- buffer.object_id.should eq(args.last.object_id)
+ buffer.should eq('foo')
end
end
context 'when the template has locals and no arguments' do
let(:template){ Template.new(hello_with_data_path) }
let(:args){ [] }
it 'uses template locals in Scope' do
- scope.should be_a(Scope::ObjectScope)
- scope.subject.should eq("who" => "world")
+ scope.subjects.should eq([{"who" => "world"}])
+ end
+ it 'uses a string buffer' do
+ buffer.should eq('')
+ end
+ end
+
+ context 'when the template has locals and arguments' do
+ let(:template){ Template.new(hello_with_data_path) }
+ let(:args){ [ 12, {}, 'foo' ] }
+ it 'sets the correct scoping subjects' do
+ scope.subjects.should eq([12, {}, {"who" => "world"}])
+ end
+ it 'uses the string as buffer' do
+ buffer.should eq('foo')
end
end

0 comments on commit f5ec0c5

Please sign in to comment.