Skip to content

Commit ced9f36

Browse files
committedMar 12, 2015
Initial (recommit) of template local variable support
1 parent a7d1525 commit ced9f36

File tree

7 files changed

+43
-4
lines changed

7 files changed

+43
-4
lines changed
 

‎lib/opal/builder_processors.rb

+8
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ def prepare(source, path)
132132
erb_compiler = erb_compiler_class.new(source, path)
133133
erb_compiler.prepared_source
134134
end
135+
136+
def compiled
137+
@compiled ||= begin
138+
compiler = compiler_for(@source, file: @filename, template: true)
139+
compiler.compile
140+
compiler
141+
end
142+
end
135143
end
136144

137145
class ERBProcessor < Processor

‎lib/opal/compiler.rb

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ def self.compiler_option(name, default_value, options = {})
8686
# compile top level local vars with support for irb style vars
8787
compiler_option :irb, false, :as => :irb?
8888

89+
#@!method template?
90+
#
91+
# compile file with support for local variables (used by Template)
92+
compiler_option :template, false, :as => :template?
93+
8994
# @!method dynamic_require_severity
9095
#
9196
# how to handle dynamic requires (:error, :warning, :ignore)

‎lib/opal/erb.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def prepared_source
4646
end
4747

4848
def compile
49-
Opal.compile prepared_source
49+
Opal.compile prepared_source, :template => true
5050
end
5151

5252
def fix_quotes(result)
@@ -77,7 +77,7 @@ def find_code(result)
7777

7878
def wrap_compiled(result)
7979
path = @file_name.sub(/\.opalerb$/, '')
80-
result = "Template.new('#{path}') do |output_buffer|\noutput_buffer.append(\"#{result}\")\noutput_buffer.join\nend\n"
80+
result = "Template.new('#{path}') do |output_buffer, __locals = {}|\noutput_buffer.append(\"#{result}\")\noutput_buffer.join\nend\n"
8181
end
8282
end
8383
end

‎lib/opal/nodes/call.rb

+16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def compile
3333
# if trying to access an lvar in irb mode
3434
return compile_irb_var if using_irb?
3535

36+
# if compiling a template, support local variables
37+
return compile_template_var if using_template?
38+
3639
default_compile
3740
end
3841

@@ -121,6 +124,19 @@ def using_irb?
121124
@compiler.irb? and scope.top? and arglist == s(:arglist) and recvr.nil? and iter.nil?
122125
end
123126

127+
def using_template?
128+
@compiler.template? and scope.iter? and arglist == s(:arglist) and recvr.nil? and iter.nil?
129+
end
130+
131+
def compile_template_var
132+
with_temp do |tmp|
133+
lvar = variable(meth)
134+
call = s(:call, s(:self), meth.intern, s(:arglist))
135+
push "((#{tmp} = __locals.smap.#{lvar}) == null ? ", expr(call),
136+
" : #{tmp})"
137+
end
138+
end
139+
124140
# Handle "special" method calls, e.g. require(). Subclasses can override
125141
# this method. If this method returns nil, then the method will continue
126142
# to be generated by CallNode.

‎spec/opal/stdlib/erb/erb_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
require File.expand_path('../simple', __FILE__)
33
require File.expand_path('../quoted', __FILE__)
44
require File.expand_path('../inline_block', __FILE__)
5+
require File.expand_path('../with_locals', __FILE__)
56

67
describe "ERB files" do
78
before :each do
89
@simple = Template['opal/stdlib/erb/simple']
910
@quoted = Template['opal/stdlib/erb/quoted']
1011
@inline_block = Template['opal/stdlib/erb/inline_block']
12+
@with_locals = Template['opal/stdlib/erb/with_locals']
1113
end
1214

1315
it "should be defined by their filename on Template namespace" do
@@ -27,4 +29,11 @@
2729
it "should be able to handle inline blocks" do
2830
@inline_block.should be_kind_of(Template)
2931
end
32+
33+
describe 'template locals' do
34+
it 'can pass local variables to a template' do
35+
def self.non_local; 'Ford'; end
36+
@with_locals.render(self, :is_local => 'Perfect').should =~ /Ford\ Perfect/
37+
end
38+
end
3039
end
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= non_local %> <%= is_local %>

‎stdlib/template.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def inspect
2323
"#<Template: '#@name'>"
2424
end
2525

26-
def render(ctx = self)
27-
ctx.instance_exec(OutputBuffer.new, &@body)
26+
def render(ctx = self, locals = {})
27+
ctx.instance_exec(OutputBuffer.new, locals, &@body)
2828
end
2929

3030
class OutputBuffer

0 commit comments

Comments
 (0)
Please sign in to comment.