mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Add :locals option for erb. General approach and some of the code was borrowed from rails (compilable.rb)
This commit is contained in:
parent
07f2547331
commit
f2733e8785
2 changed files with 32 additions and 2 deletions
|
@ -576,9 +576,19 @@ module Sinatra
|
|||
private
|
||||
|
||||
def render_erb(content, options = {})
|
||||
::ERB.new(content).result(binding)
|
||||
locals_opt = options.delete(:locals) || {}
|
||||
|
||||
locals_code = ""
|
||||
locals_hash = {}
|
||||
locals_opt.each do |key, value|
|
||||
locals_code << "#{key} = locals_hash[:#{key}]\n"
|
||||
locals_hash[:"#{key}"] = value
|
||||
end
|
||||
|
||||
body = ::ERB.new(content).src
|
||||
eval("#{locals_code}#{body}", binding)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
module Haml
|
||||
|
|
|
@ -23,6 +23,26 @@ context "Erb" do
|
|||
body.should == '2'
|
||||
|
||||
end
|
||||
|
||||
specify "should take an options hash with :locals set with a string" do
|
||||
get '/locals' do
|
||||
erb '<%= foo %>', :locals => {:foo => "Bar"}
|
||||
end
|
||||
|
||||
get_it '/locals'
|
||||
should.be.ok
|
||||
body.should == 'Bar'
|
||||
end
|
||||
|
||||
specify "should take an options hash with :locals set with a complex object" do
|
||||
get '/locals-complex' do
|
||||
erb '<%= foo[0] %>', :locals => {:foo => ["foo", "bar", "baz"]}
|
||||
end
|
||||
|
||||
get_it '/locals-complex'
|
||||
should.be.ok
|
||||
body.should == 'foo'
|
||||
end
|
||||
end
|
||||
|
||||
context "with layouts" do
|
||||
|
|
Loading…
Reference in a new issue