|
|||||||
このドキュメントで説明している機能は、V1.0ではFIXしてないものです。 今後、変更される可能性があります。
コード:
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)
出力:
<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>
このようなクラスがあったとします。
class Header
attr_reader :title
def initialize(title)
@title = title
end
end
次のようなテンプレートを書いて、 クラスにテンプレートを「インストール」すると…
<span class=Header>
<h1 id=title></h1>
</span>
Headerクラスは、このテンプレートに従って、自分自身を展開して表示するこ とができるようになります。
h = Header.new("Scripting Languages")
puts h.to_s # => <h1>Scripting Languages</h1>
もし、このオブジェクトをモデルデータの一部分として使用すると、 HTMLドキュメントに埋めこまれます。
別のモジュールにテンプレートをインストールして、 実行時に使用するモジュールを選択することも可能です。
詳細は下記を参照してください。
sample/tour/parts2.rb
コード:
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)
出力:
<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>
tmpl.expand_attr = true
このアトリビュートが設定されていると、 "@url"のような"@"で始まる文字が設定された属性をチェックします。
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".
CGIKit(www.spice-of-life.net/download/cgikit/index_en.html) は RubyによってCGI アプリケーションを開発するためのフレームワークです。
amritaには、CGIKitとのインターフェースがあります。CKAmritaElement を他 の標準のコンポーネントと同様に使用することで、CGIアプリケーションを手早く開発することができます。
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!
ckdファイルでCKAmritaElementに割りあてられた<CGIKIT>...</CGIKIT>の間のHTMLソースは、 amritaにテンプレートとして渡されます。
それ以外のHTMLソースはCGIKitが普通通りに処理します。 ですから、CGIKitの標準コンポーネントと amrita(CKAmritaElement: amritaのテンプレートエンジンを含むCGIKitのコンポーネント)を混在させることが可能になります。
CKAmritaElement は モデルデータをCGIKitのコンポーネントから取り出します。 具体的には以下のデータを使用します。
* メインページオブジェクト(親コンポーネント)のメソッド
+id+ 属性はメソッドサーチに使用されます。
ckdファイルで指定することにより他のメソッド名を使用することもできます。
* ckdファイルで静的に設定された値 * 他のコンポーネントによって動的に設定された値
FORMやアクションにはCGIKitを使用して、結果表示にamritaを使うのがよいと思います。 (CGIKitではフォーム内のボタンをRubyメソッドにバインドできます)
詳細は sample/cgikit/Examples を参照してください。
Amrita Script は テンプレートの中にモデルデータを含める実験的な機能です。
コード:
<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>
出力:
$ 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>
ams というコマンド(bin/amsとしてamritaのアーカイブに含まれています)は <amritascript> <!-- ... //--></amritascript> の中のテキストを抜き出して、 Rubyのコードとして評価します。 そして、その評価結果をモデルデータとしてamritaが使用します。
amx(AMrita eXtention for XML)はXML用スタイルシートです。 XMLドキュメントをHTMLに変換します。 出力の形式を指定するのにamritaのテンプレートが使用できます。
source document:
<?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>
amxを使用するには、下記のコマンドを使用してください。
ソースドキュメントは、整形式(well-formed)のXMLなら、どんなXML文書でもかまいません。 ただし、先頭に以下の指示を含む必要があります。
<?amx href="index.amx" ?>
href属性で、amxテンプレートへのパスを指定します。
amxは指定されたテンプレートファイルを読みこみ、そこからテンプレートオブジェクトを生成します。 テンプレートファイルは特別なXML文書で、以下の二つのパートに分かれています。
これは(XHTMLで記述された)普通のamritaのテンプレートです
この要素を使用すると、テンプレートオブジェクトにメソッドを追加できます。
テンプレートオブジェクトは、Amx::Template クラスのオブジェクトです。 REXMLのDOMツリーとしてロードされたソースドキュメントからモデルデータを作成します。
amritaホームページのトップページはamxで生成されています。
amxの詳細な使用はまだ決定していません。
もし、amxを使おうと思ったら、気軽に私にメールしてください。
* ソースXML文書 * HTML文書(こういうふうに変換したいというもの)
をもらえたら、どうやったらいいかわかるようなサンプルを作成して、次のリリースに含めます。
サンプルがたくさん集まったら、ちゃんとamxのドキュメントを書いて仕様を決定します。