File: Tour2

Tour2 |
Path: | docs/Tour2
Modified: | Wed Nov 06 16:54:25 JST 2002

# Amrita tour2

This document describes features not fixed until V1.0. So these features may be modified later.

## parts tempalte (experimental)

### code and output

code:

      require 'amrita/parts'
      include Amrita

      module Elements
        class Header
          attr_reader :title
          def initialize(title)
            @title = title
          end
        end

        class List
          attr_reader :list
          def initialize(list)
            @list = list
          end
        end

        class RowData
          attr_reader :lang, :author, :url
          def initialize(lang, author, url)
            @lang, @author, @url = lang, author, url
          end

          def url_with_link
            e(:a, :href=>url) { url }
          end
        end
      end
      include Elements

      parts_template = TemplateText.new <<END
      <span class=Header>
        <h1 id=title></h1>
      </span>

      <span class=List>
        <ul>
          <li id=list>
        </ul>
      </span>

      <span class=RowData>
        <tr>
          <td id=lang><td id=author><td id=url_with_link>
        </tr>
      </span>
      END

      parts_template.install_parts_to(Elements)

      document_template = TemplateText.new <<END
      <html>
      <body>
        <span id=header></span>
        <span id=list></span>

        <table>
          <span id=tabledata></span>
        </table>
      </body>
      END

      data = {
        :header=>Header.new("Scripting Languages"),
        :list=>List.new(%w(Ruby Perl Python)),
        :tabledata=> [
          RowData.new("Ruby", "matz", "http://www.ruby-lang.org/"),
          RowData.new("perl", "Larry Wall", "http://www.perl.com/"),
          RowData.new("python", "Guido van Rossum", "http://www.python.org/")
        ]
      }

      document_template.prettyprint = true
      document_template.expand(STDOUT, data)

output:

        <html>
          <body>
            <h1>Scripting Languages</h1>
            <ul>
              <li>Ruby</li>
              <li>Perl</li>
              <li>Python</li>
            </ul>
            <table>
              <tr>
              <td>Ruby</td>
              <td>matz</td>
              <td><a href="http://www.ruby-lang.org/">http://www.ruby-lang.org/</a></td>
              </tr>
              <tr>
              <td>perl</td>
              <td>Larry Wall</td>
              <td><a href="http://www.perl.com/">http://www.perl.com/</a></td>
              </tr>
              <tr>
              <td>python</td>
              <td>Guido van Rossum</td>
              <td><a href="http://www.python.org/">http://www.python.org/</a></td>
              </tr>
            </table>
          </body>
        </html>

### description

If you want to add a presentation to a Class like this,

      class Header
        attr_reader :title
        def initialize(title)
          @title = title
        end
      end

write a template for it and install the template to the Class.

      <span class=Header>
        <h1 id=title></h1>
      </span>

Now, the Header object has the ability to show itself as a HTML data

       h = Header.new("Scripting Languages")

       puts h.to_s # => <h1>Scripting Languages</h1>

And if it was used as a part of model data, it will be embeded in the HTML document.

You can install the parts template in another module and select view-module at runtime. For detail see sample/tour/parts2.rb

## attribute expand (experimental)

### 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="title" href="@url"></a></td>
        </tr>
      </table>
      END

      data = {
         :table1=>[
          {
            :name=>"Ruby",
            :author=>"matz" ,
            :url=>"http://www.ruby-lang.org/",
            :title=>"Ruby Home Page"
          },
          {
            :name=>"perl",
            :author=>"Larry Wall" ,
            :url=>"http://www.perl.com/",
            :title=>"Perl.com"
          },
          {
            :name=>"python",
            :author=>"Guido van Rossum" ,
            :url=>"http://www.python.org/",
            :title=>"Python Language Website"
          },
         ]
      }
      tmpl.prettyprint = true
      tmpl.use_compiler = true
      tmpl.expand_attr = true
      tmpl.set_hint_by_sample_data(data)
      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

       tmpl.expand_attr = true

If this attribute was set, then amrita checks all attribute values and convert it with model data if the value in template begins "@" like "@url".

## merge two template (experimental)

You can merge template with Amrita::MergeTemplate

### code and output

code:

      require "amrita/template"
      require "amrita/merge"
      include Amrita

       This idea was suggested by Tom Sawyer

      tmpfile = "/tmp/html1.html"

      File::open(tmpfile, "w") do |f|
        f.write <<-END
        <html>
          <head>
            <title>Insertable</title>
          </head>
          <body>
            <span id="insert_me"><b>Hello World!</b></span>
          </body>
        </html>
        END
      end

      tmpl = TemplateText.new <<-END
        <html>
          <head>
            <title>Insertion MockUp</title>
          </head>
          <body id="data">
            This comes from a template fragment:
            <span id="#{tmpfile}#insert_me">This will be replaced.</span>
          </body>
        </html>
      END

      model_data = { :data => MergeTemplate.new}
      tmpl.expand(STDOUT, model_data)

      File::unlink tmpfile

      __END__

      the output of file2, when passed through Amrita, would then be:

        <html>
          <head>
            <title>Insertion MockUp</title>
          </head>
          <body>
            This comes from a template fragment:
            <span><b>Hello World!</b></span>
          </body>
        </html>

output:

      <html>
        <head>
          <title>Insertion MockUp</title>
        </head>
        <body>
          This comes from a template fragment:
          <b>Hello World!</b>
        </body>
      </html>

### description

        <body id="data">
          This comes from a template fragment:
          <span id="another_template.html#insert_me">This will be replaced.</span>
        </body>

        model_data = { :data => MergeTemplate.new}
        tmpl.expand(STDOUT, model_data)

MergeTemplate is a special model object that takes `id="filenametag"` as template insertion.

MergeTemplate read from `filename` and search `id` with `tag` and insert it to the original temaplte.

You can recursive expansion to the merged template. For detail see sample/tour/merge_tempalte2.rb

## cgikit interface (experimental)

CGIKit(www.spice-of-life.net/download/cgikit/index_en.html) is a CGI application framework with Ruby. This framework bases component for development, you will develop CGI application speedy and efficiency.

amrita has an interface to cgikit. You can use CKAmritaElement with other standard componet of cgikt.

### code and output

HelloWorld.cgi:

        require 'amrita/cgikit'

        app = CKApplication.instance
        app.run

MainPage/MainPage.html

        <html>
        <head>
        <title>Hello World</title>
        </head>
        <body>

        <h1>
        <CGIKIT NAME=HelloWorld>
          <span id="hello"></span>
        </CGIKIT>
        </h1>

        </body>
        </html>

MainPage/MainPage.ckd

        HelloWorld : CKAmritaElement {
          hello = sayHello;
        }

MainPage/MainPage.rb

        class MainPage < CKComponent
            def sayHello
                    "Hello World!"
            end
        end

output:

        Hello World!

### description

The html source between <CGIKIT>...</CGIKIT> is provided as the template for amrita if the NAME for it is assined to CKAmritaElement in ckd file.

Other source is treated by cgikit as usual. So you can use amrita (CKAmritaElement: a cgikit componet with amrita template engine) with other cgikit's standard componets.

CKAmritaElement gets the model data from cgikit.

      * the method of main page object ( parent componet )

        the +id+ attribute value is use for method search.
        you can assign different name for it in ckd file.

      * a value assigned in ckd file

      * a value dynamically assinged by other componets

I think it's better to use cgikit for FORM and action ( a button can be binded to a Ruby method easily) and use amrita for displaying the result.

see sample/cgikit/Examples for detail.

## Amrita Script (experimental)

AmritaScript is an experimental feature that packs a template with the model data for it.

### code and output

code:

      <html>
        <amritascript> <!--
          data = {
             :title => "hello world",
             :body => "Amrita is a html template libraly for Ruby",
             :time => Time.now,
             :modified => File::stat($amrita_template_path).mtime
          }
         //--></amritascript>

         <body>
            <h1 id=title>title will be inserted here</h1>
            <p id=body>body text will be inserted here</p>
            <hr>
            <span id=time></span>/
            last-modified <span id=modified></span>
         </body>
      </html>

output:

      $ ams amstest.ams

      <html>
        <body>
          <h1>hello world</h1>
          <p>Amrita is a html template libraly for Ruby</p>
          <hr>
          Wed Aug 07 18:12:38 JST 2002/
          last-modified Wed Aug 07 08:44:33 JST 2002
         </body>
      </html>

### description

The command `ams` (provided at bin/ams with amrita) extracts text in `<amritascript> <!-- ... //--></amritascript>` and `eval` as a Ruby code to use it as a model data for amrita.

## amx: yet anothor style-sheet for XML (experimental)

amx(AMrita eXtention for XML) is a style-sheet for XML. It converts an XML document to HTML. You can use amrita template for specifing the output format.

### code and output

source document:

      <?xml version="1.0" ?>
      <?amx href="amxtest.amx" ?>
      <document>
        <head>
          <title>amx sample</title>
        </head>
        <body>
          <paragraph>
            amx is a XML document.
            It contains model data as well-formed XML, HTML template
            and a small Ruby code map both.
          </paragraph>
          <paragraph>
            This is a sample AMX document.
          </paragraph>
        </body>
      </document>

template:

      <amx>
        <template>
          <html>
            <body>
              <h1 id="title">title will be inserted here</h1>
              <span id="body">
                <p id="paragraph">body text will be inserted here</p>
              </span>
              <hr />
              <span id="time" />
            </body>
          </html>
        </template>

        <method id="get_model">
          <method_body>
            {
               :title => doc.elements['document/head/title'],
               :body => {
                 :paragraph => doc.elements.to_a('document/body/paragraph').collect do |n|
                   n.get_text
                 end
               },
               :time => Time.now
            }
          </method_body>
        </method>
      </amx>

output:

       <html>
          <body>
            <title>amx sample</title>

              <p>
          amx is a XML document.
          It contains model data as well-formed XML, HTML template
          and a small Ruby code map both.
        </p><p>
          This is a sample AMX document.
        </p>

            <hr />
            Fri Aug 23 13:35:02 JST 2002
          </body>
        </html>

### description

To use amx, type this command from command line

       $ amx amxtest.xml

The source document can be any well-formed XML document with an instruction

      <?amx href="index.amx" ?>

href attribute is the path for amx-template.

amx loads the template file and generate a template object from it. The template file is a special XML document and has two parts

- <template> element

  this is a normal amrita template

- <method> element

  by this element, you can define a method in the template object.

the template object is a Amx::Template object. It will make a model data from source document loaded as a REXML DOM tree.

### more information

The top page of amrita is generated by amx.

docs/index.xml
  source document
docs/index.amx
  template document
docs/amritadoc.rb
  additional Ruby code

detail spec of amx is not fixed yet.

If you want to use amx, feel free to mail me. I will make a sample for you if you gave me ....

- source XML document
- desired output HTML document

These samples will be attached to the next release of amrita archive.

I will fix the spec and write a document for this feature after I've got enough requests and samples.

## X

### code and output

code:

output:

### description