= Amrita tour
== modify attributes of HTML element
=== code and output
code:
:include: sample/tour/makeurl.rb
output:
=== description
The Amrita#a() method produce a Amrita::AttrArray object.
a(:href=>"http://www.ruby-lang.org/") { "Ruby Home Page" },
When this special object is used for a model data, it modifies HTML
element's attributes and set text. So if template for this data is ...
|
The output will be....
Ruby Home Page |
filelist.rb described in docs/XML uses AttrArray object too.
There is another way to do this, see expand attribute expand in docs/Tour2
---
== proc
You can give a +proc+ as model data to edit element manualy.
=== code and output
code:
:include: sample/tour/proc.rb
output:
- java
- I love Ruby!
- perl
...
=== description
If model data is a Proc object, Amrita calls it with Amrita::Element
object that represents the HTML element.Amrita will replace the
element by result of +proc+.
In that proc, you can edit Element freely.
setting an attribute
elem[:color] = "red"
setting the text of element
elem.set_text("I love Ruby!")
generate a new Element with Amrita#e method
e(:em) { elem }
In this case +elem+ is I love Ruby!.
The output is wrapped by \....\ by e(:em) { .... }.
---
== use custom classes for model data
=== code and output
code:
:include: sample/tour/time.rb
output:
2002/7/17
=== description
If the model data is +kind_of+ Amrita::ExpandByMember, amrita uses
+id+ value as a method and call method of that name.
In this example, the data for +:time+ is a Ruby's standard Time object
but it +extend+ ExpandByMember. So +id+'s value +year+ is treated as
a method name and amrita calls that method of +t+.
So output for tempalte get result of
method call t.yera: "2002" . Thus the produces the output...
2002/7/17
Amrita deletes element if there is no attributes after
deleteing +id+ attribute.So last output is
2002/7/17
---
== precompile
Amrita can compile HTML template to Ruby code before +expand+.
=== code and output
code(the added code to table.rb) :
tmpl = TemplateText.new(TEMPLATE)
tmpl.use_compiler = true
tmpl.set_hint_by_sample_data(data) # optional: optimization to that data
tmpl.expand(STDOUT, data) # with compiled code
puts "----code generated by Amrita -----------"
puts tmpl.src
puts "----code generated by Amrita end -------"
The output is same as table.rb with the benchmark report added.
Here's my data on a Crusoe TM5600.
43.068354 seconds for 1000 times without compiling
5.078764 seconds for 1000 times with pre-compiled code
=== description
You only add one line for compiling
tmpl.use_compiler = true
After this, +expand+ method will be executed by compiled code that
produce (almost) same output.
And optionally give a sample data to amrita.
tmpl.set_hint_by_sample_data(data)
Amrita::HTMLCompiler uses this sample data for optimizing the output code.
So, if data structure changes after it, you must call
+set_hint_by_sample_data+ again.
Amrita::HTMLCompiler can produce a code that include interpreter mode
partially. If you need to compile and some part of model may change
dynamically, you can give +nil+ for data that may change.
Amrita::Compiler call Element::expand method in compiled code at that
point.
You can take trade off of speed and flexibility at any point you like.
---
== Sanitizing -- anti XSS attack
Amrita has a built in Amrita::Sanitizer to protect against XSS(cross
site scripting) attacks. Amrita::Formatter uses this module
automaticaly.
=== code and output
:include: sample/tour/sanitizer.rb
=== description
==== text
The dangerous characters for xhtml/html text (<>&) are escaped.
"" => "<abc>"
==== attribute value
The dangerous characters for attribute value (<>&"') are escaped.
==== special attribute value for URL
These attribute should be treated in another way because they would
have a URL value
* +href+ attribute of element
* +src+ attribute of
element
* +action+ attribute of