Take the 2008 Git User's Survey and help out! [ hide ]

public
Rubygem
Description: Classy web-development dressed in a DSL
Homepage: http://sinatrarb.com
Clone URL: git://github.com/bmizerany/sinatra.git
Search Repo:
Add :locals option for erb.  General approach and some of the code was 
borrowed from rails (compilable.rb)
cschneid (author)
Tue May 27 19:09:45 -0700 2008
bmizerany (committer)
Tue May 27 19:16:23 -0700 2008
commit  f2733e8785b8b943f4114d4a8ff25b5ef632a1ff
tree    1c063b35a5cdc7c30bf0d373d59fa2a05ac078d9
parent  07f254733192d114d59143b997ee51a4ca003a48
...
576
577
578
579
 
 
 
 
 
 
 
 
 
 
 
580
581
 
582
583
584
...
576
577
578
 
579
580
581
582
583
584
585
586
587
588
589
590
 
591
592
593
594
0
@@ -576,9 +576,19 @@ module Sinatra
0
     private
0
     
0
       def render_erb(content, options = {})
0
- ::ERB.new(content).result(binding)
0
+ locals_opt = options.delete(:locals) || {}
0
+
0
+ locals_code = ""
0
+ locals_hash = {}
0
+ locals_opt.each do |key, value|
0
+ locals_code << "#{key} = locals_hash[:#{key}]\n"
0
+ locals_hash[:"#{key}"] = value
0
+ end
0
+
0
+ body = ::ERB.new(content).src
0
+ eval("#{locals_code}#{body}", binding)
0
       end
0
-
0
+
0
   end
0
 
0
   module Haml
...
23
24
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
27
28
...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -23,6 +23,26 @@ context "Erb" do
0
       body.should == '2'
0
 
0
     end
0
+
0
+ specify "should take an options hash with :locals set with a string" do
0
+ get '/locals' do
0
+ erb '<%= foo %>', :locals => {:foo => "Bar"}
0
+ end
0
+
0
+ get_it '/locals'
0
+ should.be.ok
0
+ body.should == 'Bar'
0
+ end
0
+
0
+ specify "should take an options hash with :locals set with a complex object" do
0
+ get '/locals-complex' do
0
+ erb '<%= foo[0] %>', :locals => {:foo => ["foo", "bar", "baz"]}
0
+ end
0
+
0
+ get_it '/locals-complex'
0
+ should.be.ok
0
+ body.should == 'foo'
0
+ end
0
   end
0
   
0
   context "with layouts" do

Comments

    No one has commented yet.