From tnakajima at brain-tokyo.jp Mon Jun 13 02:53:08 2005 From: tnakajima at brain-tokyo.jp (Taku Nakajima) Date: Mon Jun 13 02:48:53 2005 Subject: [Amrita2-users] new ERB integration Message-ID: <878y1epx2j.wl@brain-tokyo.jp> Hi, amrita2-users! I've got a new idea of integration ERB with Amrita2 and commited a test code for it just now. I think Amrita2 will be fit better with Rails and you can use helper methods of Rails in Amrita2 template. I will complete the scaffold generator using ths feature until next release. This idea consists of some minor ideas. I will explain them step by step. The samples are taken from test/test_erb.rb in the repository. This dose not pass all the test codes now, but I will fix it soon. == Binding as Presentation Object Amrita2 will accept Binding Object now as well as Hash and Structs. t = TemplateText.new "" a = 1 t.expand(out="", binding) assert_equal("1", out) "binding" is a standard Ruby method which return a Binding Object. If it was given for template expansion, Amrita2 will eval data for it's sub elements like this. # b is a Binding Object given eval("a", b) "a" will evaluated as a local variable name or a method name which can be seen at the place where "binding" was called. In this case, there is a local variable named as "a", so the value of it will be used. == with no attribute will be deleted Now tag with no attribute will be deleted at template expansion. t = TemplateText.new "" a = 1 b = 2 t.expand(out="", binding) assert_equal("12", out) This add more compatiblity with Amrita1.0. == CDATA section is evaluated as an ERB source I added a new option 'eval_cdata_as_erb'. If this option was set, all the section of the template will be evaluated as ERB. t = TemplateText.new "]]>]]>" t.eval_cdata_as_erb = true t.expand(out="", :a=>1) assert_equal("31XX", out) == You can use one Binding for both old-Amrita2-style element and new-ERB-style element def em(s) %[#{s}] end def test_erb4 t = TemplateText.new "
]]>
" t.eval_cdata_as_erb = true a = 1 b = 2 t.expand(out="", binding) assert_equal("
12
", out) end In this case, "binding" was called in the test_erb3 where local variable "a" and "b" and a method named "em" are available. The variable "b" is used by "" The value of "b" is 2 so the result is "2" The method "em" and te variable "a" is used in "]]>" The value of "a" is 1 and the method "em" was called with it, so, the result is "1". == context-data If CData section was in an element with id, you can use a variable '$_' to which the data of PO will be assigned. t = TemplateText.new "]]>" a = 1 b = 2 t.eval_cdata_as_erb = true t.expand(out="", binding) assert_equal("3", out) So you can use ERB in a Amrita-style loop. t = TemplateText.new "
  • ]]>
" list = 0..2 t.eval_cdata_as_erb = true t.expand(out="", binding) assert_equal("
  • 10
  • 20
  • 30
", out) == a more complex example def test_table t = TemplateText.new <<-END
]]> 20 ? xxx + em($_[:xxx]) + $_[:aaa] : $_[:xxx] %>]]>
END xxx = 'xxx' table_data = [ { :aaa=>'a1', :bbb=>'b1 ', :ccc=>'c1', :xxx=>10}, { :aaa=>'a2', :bbb=>'b2 ', :ccc=>'c2', :xxx=>20}, { :aaa=>'a3', :bbb=>'b3 ', :ccc=>'c3', :xxx=>30}, ] t.eval_cdata_as_erb = true t.expand(out="", binding) assert_equal_xml(<<-END, out)
a1 b1 c1 10
a2 b2 c2 20
a3 b3 c3 xxx30a3
END end The last column dose not have id, so the context-data($_) for it is a Hash template: 20 ? xxx + em($_[:xxx]) + $_[:aaa] : $_[:xxx] %>]]> $_ is a Hash like { :aaa=>'a1', :bbb=>'b1 ', :ccc=>'c1', :xxx=>10}, The result of this column is first: 10 socond: 20 third: xxx30a3 -- Taku Nakajima