Path: | docs/QuickStart |
Last Update: | Thu Jul 18 00:08:15 +0000 2002 |
Amrita has two level APIs. This sample shows how to use Amrita‘s high level API: Amrita::TemplateFile and Amrita::TemplateText. They both are derived from Amrita::Template which wraps Amrita‘s low level API.
The sample codes in this document use only high level API.
This is the simplest template for Amrita
<html> <body> <h1 id=title>title will be inserted here</h1> <p id=body>body text will be inserted here</p> </body> </html>
Amrita treats an element with id attribute as a dynamic element and will get the data for it from model data by id attribute‘s value as key.
This is the code that use the template above and produce an output to STDOUT.
require "amrita/template" include Amrita tmpl = TemplateFile.new("template.html") data = { :title => "hello world", :body => "Amrita is a html template libraly for Ruby" } tmpl.expand(STDOUT, data)
Amrita::Template mixes template with model data and produces output html document.
The output of this code is …
<html> <body> <h1>hello world</h1> <p>Amrita is a html template library for Ruby</p> </body> </html>
The text "hello world" is picked up from model data by the key title and inserted into <h1> which has id="title" attribute. And <p id="body">…</p> was modified in the same way.
You can use Amrita in these steps.
tmpl = TemplateFile.new("template.html")
data = { :title => "hello world", :body => "Amrita is a html template library for Ruby" }
Model data can be various form but should be fit template‘s ID structure. In this case, template has two +id+s and they has value "title" and "body". So model data must provide data for "title" and "body".
tmpl.expand(STDOUT, data)
The first parameter of expand is the stream: amrita will put the output to it by << method. stream can be IO including File, or String or Array or any objects that has << method.