17. ‣ PHP, Java, JS, C, Perl,Scheme ( )
<% for (i=0; i<n; i++) { %> (C )
<li><%= "%d", i %> printf()
<% } %>
#line 1 "file.ec" (erubis -xl c file.ec )
for (i=0; i<n; i++) {
fputs("<li>", stdout); fprintf(stdout, "%d", i);
fputs("n", stdout); }
copyright(c) 2009 kuwata-lab.com all rights reserved.
17
18. ‣ Erubis
• HTML
•
• Enhancer
• /
•
• PHP, Java, JS, C, Perl, Scheme
copyright(c) 2009 kuwata-lab.com all rights reserved.
18
19. Part 2. eRuby
copyright(c) 2009 kuwata-lab.com all rights reserved.
19
20. ‣ binding()
•
i=0 ### file.erb
str = File.read('file.erb') <% for i in 1..3 %>
ERB.new(str).result(binding) <li><%= i %></li>
p i #=> 3 <% end %>
!
copyright(c) 2009 kuwata-lab.com all rights reserved.
20
21. ‣ binding()
•
•
b = Bingind.new
b[:title] = "Example" …
b[:items] = [1, 2, 3]
copyright(c) 2009 kuwata-lab.com all rights reserved.
21
22. ERB
‣( )
‣ Struct
• http://d.hatena.ne.jp/m_seki/20080528/1211909590
Foo = Struct.new(:title, :items)
class Foo
def env; binding(); end
end
ctx = Foo.new("Example", [1,2,3])
ERB.new(str).result(ctx.env)
copyright(c) 2009 kuwata-lab.com all rights reserved.
22
23. Erubis
‣ Binding Hash
erubis.result(:items=>[1, 2, 3])
def result(b=TOPLEVEL_BINDING)
if b.is_a?(Hash)
s = b.collect{|k,v| "#{k}=b[#{k.inspect}];"}.join
b = binding()
eval s, b Binding
end
return eval(@src, b)
end
copyright(c) 2009 kuwata-lab.com all rights reserved.
23
24. Erubis
‣ Binding Object
@items = [1, 2, 3]; <% for x in @items %>
erubis.evaluate(self) <% end %>
def evaluate(ctx) Hash
if ctx.is_a?(Hash)
hash = ctx; ctx = Object.new
hash.each {|k,v|
ctx.instance_variable_set("@#{k}", v) }
end
return ctx.instance_eval(@src)
end copyright(c) 2009 kuwata-lab.com all rights reserved.
24
28. Erubis
‣ Proc
•
•
instance_eval
def evaluate(ctx) Proc
@proc ||= eval(@src)
ctx.instance_eval(@proc)
end
copyright(c) 2009 kuwata-lab.com all rights reserved.
28
29. ‣
• HTML
<ul>
<ul>
<% for x in @list %> <li>AAA</li>
<li><%= x %></li>
<% end %> <li>BBB</li>
</ul>
</ul>
copyright(c) 2009 kuwata-lab.com all rights reserved.
29
31. Erubis
‣
• <% %>
• <%= %>
<ul> <ul>
<% for x in @list %> AAA
<%= x %> BBB
<% end %> CCC
</ul> </ul>
copyright(c) 2009 kuwata-lab.com all rights reserved.
31
40. $ cat foo.eruby
<% unless @items.blank? %>
<table>
<% @items.each_with_index do|x, i| %>
<tr class="record">
<td><%= i +1 %></td>
<td><%=h x %></td>
</tr>
<% end %>
</table>
<% end %>
copyright(c) 2009 kuwata-lab.com all rights reserved.
40
41. -x Ruby
$ erubis -x foo.eruby
_buf = ''; unless @items.blank?
_buf << '<table>
'; @items.each_with_index do|x, i|
_buf << ' <tr class="record">
<td>'; _buf << ( i +1 ).to_s; _buf << '</td>
<td>'; _buf << (h x ).to_s; _buf << '</td>
</tr>
'; end
_buf << '</table>
'; end
_buf.to_s
copyright(c) 2009 kuwata-lab.com all rights reserved.
41
42. -X
$ erubis -X foo.eruby
_buf = ''; unless @items.blank?
@items.each_with_index do|x, i|
_buf << ( i +1 ).to_s;
_buf << (h x ).to_s;
end
end
_buf.to_s
copyright(c) 2009 kuwata-lab.com all rights reserved.
42
43. -N (number)
$ erubis -XN foo.eruby
1: _buf = ''; unless @items.blank?
2:
3: @items.each_with_index do|x, i|
4:
5: _buf << ( i +1 ).to_s;
6: _buf << (h x ).to_s;
7:
8: end
9:
10: end
11: _buf.to_s
copyright(c) 2009 kuwata-lab.com all rights reserved.
43
44. -U (uniq)
$ erubis -XNU foo.eruby
1: _buf = ''; unless @items.blank?
3: @items.each_with_index do|x, i|
5: _buf << ( i +1 ).to_s;
6: _buf << (h x ).to_s;
8: end
10: end
11: _buf.to_s
copyright(c) 2009 kuwata-lab.com all rights reserved.
44
45. -C (compact)
$ erubis -XNC foo.eruby
1: _buf = ''; unless @items.blank?
3: @items.each_with_index do|x, i|
5: _buf << ( i +1 ).to_s;
6: _buf << (h x ).to_s;
8: end
10: end
11: _buf.to_s
copyright(c) 2009 kuwata-lab.com all rights reserved.
45
46. <%= %>
<%= form_for :user do %>
<div>
<%= text_field :name %>
</div>
<% end %> eRuby
copyright(c) 2009 kuwata-lab.com all rights reserved.
46
47. <%= %>
<%= 10.times do %>
Hello
<% end %>
!
_buf = "";
_buf << ( 10.times do ).to_s;
_buf << " Hellon";
end
copyright(c) 2009 kuwata-lab.com all rights reserved.
47
48. ERB+Rails
‣ (_erbout)
!
form_for()
… _erbout
<% form_for do %> _erbout = ""
Hello form_for do
<% end %> _erbout.concat("Hello")
end
copyright(c) 2009 kuwata-lab.com all rights reserved.
48
49. Erubis+Merb
‣ Erubis
<%= form_for do %> @_buf << (form_for do;
Hello @_buf << "Hellon"
<% end =%> end);
copyright(c) 2009 kuwata-lab.com all rights reserved.
49
50. ‣ eRuby
‣ (kool!)
‣
• @_buf
‣
copyright(c) 2009 kuwata-lab.com all rights reserved.
50
51. ‣ eRuby
eRuby
•
•
•
•
• HTML
•
•
copyright(c) 2009 kuwata-lab.com all rights reserved.
51
52. Part 3.
copyright(c) 2009 kuwata-lab.com all rights reserved.
52
53. ‣
<ul> print "<ul>n"
<% for x in @a %> for x in @a
<li><%=x%></li> print "<li>#{x}</li>n"
<% end %> end
</ul> print "</u>n"
copyright(c) 2009 kuwata-lab.com all rights reserved.
53
54. <ul> s = File.read('foo.eruby')
<li><%=x%></li> e = Erubis::Eruby.new(s)
</ul> puts e.evaluate(:x=>1)
copyright(c) 2009 kuwata-lab.com all rights reserved.
54
55. ‣ ?
<%#ARGS: items, name='guest' %>
Hello <%= name %>!
<% for x in items %>
<li><%=x%></li>
<% end %>
copyright(c) 2009 kuwata-lab.com all rights reserved.
55
56. ‣ HTML
•
<html> <html>
<body>
<body> !( )
</body>
<h1><%=@title%></h1> </html>
<ul id="menulist">
<% for x in @items %> <h1><%=@title%></h1>
<li><%=x%></li> <ul id="menulist">
</ul>
<% end %>
</ul> <% for x in @items %>
</body> <li><%= x %></li>
</html> <% end %>
copyright(c) 2009 kuwata-lab.com all rights reserved.
56