--- ../sample/spec/a2tdl.rb --- |
require "amrita2/template" require "amrita2/a2tdl" include Amrita2 class PO def title "hello world" end def body { :amrita2=>'Amrita2', :ruby=>['http://www.ruby-lang.org/','Ruby' ] } end end TEMPLATE_TEXT = <<END <html> <body> <h1 id='title'>title will be inserted here</h1> <div id='body'/> </body> </html> END tmpl = TemplateText.new(TEMPLATE_TEXT) do Amrita2::define_template_spec do dynamic_element(:title) div(:body, :generate_template=>true) do em(:amrita2) span(' is a html template libraly for ') a_link(:ruby) end end end tmpl.expand(STDOUT, PO.new)output <html> <body> <h1 id='title'>hello world</h1> <div id='body'> <em id='amrita2'>Amrita2</em> <span> is a html template libraly for </span> <a href='http://www.ruby-lang.org/' id='ruby'>Ruby</a> </div> </body> </html> |
div(:body, :generate_template=>true) do em(:amrita2) span(' is a html template libraly for ') a_link(:ruby) end |
This fragment inserted in Template Spec is a A2TDL definition.
The top level of A2TDL must be div or html desribed below. It can have any tags.
If the first paramater is a Symbol, it means that the element is dynamic.
If the first paramater is a String, it is a static.
There are many Macros in A2TDL.
A2TDL has a Macro html for top level.
Using it, you can use Amrita2 without HTML template.
--- ../sample/spec/a2tdl_hello.rb --- |
require "amrita2/template" require "amrita2/a2tdl" include Amrita2 tmpl = Template.new do Amrita2::define_template_spec do html do title('hello world') div(:body, :generate_template=>true) do em(:amrita2) span(' is a html template libraly for ') a_link(:ruby) end end end end data = { :body => { :amrita2=>'Amrita2', :ruby=>['http://www.ruby-lang.org/','Ruby' ] } } tmpl.expand(STDOUT, data)output <html> <head> <title>hello world</title> </head> <body> <h1>hello world</h1> <div id='body'> <em id='amrita2'>Amrita2</em> <span> is a html template libraly for </span> <a href='http://www.ruby-lang.org/' id='ruby'>Ruby</a> </div> </body> </html> |