Tour

Path: docs/Tour
Last Update: Wed Nov 06 06:02:26 +0000 2002

Amrita tour

modify attributes of HTML element

code and output

code:

  require "amrita/template"
  include Amrita

  tmpl = TemplateText.new <<END
  <table border="1">
    <tr><th>name</th><th>author</th><th>webpage</tr>
    <tr id=table1>
      <td id="name"></td>
      <td id="author"></td>
      <td><a id="webpage"></a></td>
    </tr>
  </table>
  END

  data = {
     :table1=>[
      {
        :name=>"Ruby",
        :author=>"matz" ,
        :webpage=> a(:href=>"http://www.ruby-lang.org/") { "Ruby Home Page" },
      },
      {
        :name=>"perl",
        :author=>"Larry Wall" ,
        :webpage=> a(:href=>"http://www.perl.com/") { "Perl.com" },
      },
      {
        :name=>"python",
        :author=>"Guido van Rossum" ,
        :webpage=> a(:href=>"http://www.python.org/") { "Python Language Website" },
      },
     ]
  }
  tmpl.prettyprint = true
  #tmpl.use_compiler = true
  tmpl.expand(STDOUT, data)

output:

   <table border="1">
     <tr>
     <th>name</th>
     <th>author</th>
     <th>webpage</th>
     </tr>
     <tr>
     <td>Ruby</td>
     <td>matz</td>
     <td><a href="http://www.ruby-lang.org/">Ruby Home Page</a></td>
     </tr>
     <tr>
     <td>perl</td>
     <td>Larry Wall</td>
     <td><a href="http://www.perl.com/">Perl.com</a></td>
     </tr>
     <tr>
     <td>python</td>
     <td>Guido van Rossum</td>
     <td><a href="http://www.python.org/">Python Language Website</a></td>
     </tr>
   </table>

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 …

    <td><a id="webpage"></a></td>

The output will be.…

    <td><a href="http://www.ruby-lang.org/">Ruby Home Page</a></td>

filelist.rb described in docs/XML uses AttrArray object too.

There is another way to do this, see expand attribute expand in docs/Tour2

[Validate]