rbTenjin Examples

release: 0.7.0
last update: $Date$

Table of Contents:

Bordered Table

Template: 'table.rbhtml'
<html>
  <body>
    <h1>${@title}</h1>
    <table>
<?rb i = 0 ?>
<?rb for item in @items ?>
<?rb    i += 1 ?>
<?rb    color = i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' ?>
      <tr bgcolor="#{color}">
        <td>#{i}</td>
        <td>${item}</td>
      </tr>
<?rb end ?>
    </table>
  </body>
</html>
Convert into Ruby script:
$ rbtenjin -a convert table.rbhtml
_buf = '';  _buf << %Q`<html>
  <body>
    <h1>#{escape((@title).to_s)}</h1>
    <table>\n`
i = 0
for item in @items
   i += 1
   color = i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'
 _buf << %Q`      <tr bgcolor="#{color}">
        <td>#{i}</td>
        <td>#{escape((item).to_s)}</td>
      </tr>\n`
end
 _buf << %Q`    </table>
  </body>
</html>\n`
_buf.to_s
Main program: 'table.rb'
## create Engine object
require 'tenjin'
engine = Tenjin::Engine.new()

## render template with context data
context = { :title => 'Bordered Table Example',
            :items => [ '<AAA>', 'B&B', '"CCC"' ] }
output = engine.render('table.rbhtml', context)
puts output
Result:
$ ruby table.rb
<html>
  <body>
    <h1>Bordered Table Example</h1>
    <table>
      <tr bgcolor="#CCCCFF">
        <td>1</td>
        <td>&lt;AAA&gt;</td>
      </tr>
      <tr bgcolor="#FFCCCC">
        <td>2</td>
        <td>B&amp;B</td>
      </tr>
      <tr bgcolor="#CCCCFF">
        <td>3</td>
        <td>&quot;CCC&quot;</td>
      </tr>
    </table>
  </body>
</html>

Form

Template: 'form.rbhtml':
<?rb #@ARGS action, params, label ?>
<form action="${@action}" method="post">
  <p>
    <label>Name:</label>
    <input type="text" name="name" value="${@params[:name]}" />
  </p>
  <p>
    <label>Gender:</label>
<?rb gender = @params[:gender] ?>
    <input type="radio" name="gender" value="M" #{checked(gender=='M')} />Man
    <input type="radio" name="gender" value="W" #{checked(gender=='W')} />Woman
  </p>
  <input type="submit" value="${@label}" />
</form>
Template: 'create.rbhtml':
<?rb @title = 'Create user' ?>
<?rb @label = 'Create' ?>
<?rb @action = 'action.cgi' ?>
<?rb import(:form) ?>
Template: 'update.rbhtml':
<?rb @title = 'Update user' ?>
<?rb @label = 'Update' ?>
<?rb @action = 'update.cgi' ?>
<?rb import(:form) ?>
Layout template: 'layout.rbhtml'
<html>
 <body>
  <h1>${@title}</h1>
  <div class="main-content">
#{@_content}
  </div>
 </body>
</html>
Main program: 'main.rb':
## create Engine object
require 'tenjin'
engine = Tenjin::Engine.new(:postfix=>'.rbhtml', :layout=>'layout.rbhtml')

## render template with context data
params = { :name=>'Foo', :gender=>'M' }
context = { :params=>params }
output = engine.render(:update, context)   # :update == 'update'+postfix
puts output
Result:
$ ruby main.rb
<html>
 <body>
  <h1>Update user</h1>
  <div class="main-content">
<form action="update.cgi" method="post">
  <p>
    <label>Name:</label>
    <input type="text" name="name" value="Foo" />
  </p>
  <p>
    <label>Gender:</label>
    <input type="radio" name="gender" value="M"  checked="checked" />Man
    <input type="radio" name="gender" value="W"  />Woman
  </p>
  <input type="submit" value="Update" />
</form>

  </div>
 </body>
</html>

Preprocessing

Library: 'helper.rb'
LANGUAGES = [
  ['en', 'Engilish'],
  ['fr', 'French'],
  ['de', 'German'],
  ['es', 'Spanish'],
  ['ch', 'Chinese'],
  ['ja', 'Japanese'],
]

def link_to(label, options={})
  action, id = options[:action], options[:id]
  url = '/app'
  url << '/' << action if action && !action.empty?
  url << '/' << id     if id
  return "<a href=\"#{CGI::escape(url).gsub(/%2F/i, '/')}\">#{label}</a>"
end
Template: 'select.rbhtml'
<?RB require './helper' ?>
<form>
  <label>Select your language:</label>
  <select name="lang">
<?rb table = { @params[:lang] => ' selected="selected"' } ?>
<?RB for val, name in LANGUAGES ?>
    <option value="${{val}}" #{table[#{{val.inspect}}]}>${{name}}</option>
<?RB end ?>
  </select>
  <input type="submit" value="OK" />
</form>
<p>
  #{{link_to('Back', :action=>'index')}} |
  #{{link_to('Show '+_P('@params[:name]'), :action=>'show', :id=>_p('@params[:id]'))}}
</p>
Preprocessed code:
$ rbtenjin -a preprocess select.rbhtml
<form>
  <label>Select your language:</label>
  <select name="lang">
<?rb table = { @params[:lang] => ' selected="selected"' } ?>
    <option value="en" #{table["en"]}>Engilish</option>
    <option value="fr" #{table["fr"]}>French</option>
    <option value="de" #{table["de"]}>German</option>
    <option value="es" #{table["es"]}>Spanish</option>
    <option value="ch" #{table["ch"]}>Chinese</option>
    <option value="ja" #{table["ja"]}>Japanese</option>
  </select>
  <input type="submit" value="OK" />
</form>
<p>
  <a href="/app/index">Back</a> |
  <a href="/app/show/#{@params[:id]}">Show ${@params[:name]}</a>
</p>
Main program: 'main.rb'
require './helper'

## create engine
require 'tenjin'
engine = Tenjin::Engine.new(:postfix=>'.rbhtml', :preprocess=>true)

## render template with context data
params = { :id=>1234, :name=>'Foo', :lang=>'ch' }
context = { :params => params }
output = engine.render(:select, context)
puts output
Result:
$ ruby main.rb
<form>
  <label>Select your language:</label>
  <select name="lang">
    <option value="en" >Engilish</option>
    <option value="fr" >French</option>
    <option value="de" >German</option>
    <option value="es" >Spanish</option>
    <option value="ch"  selected="selected">Chinese</option>
    <option value="ja" >Japanese</option>
  </select>
  <input type="submit" value="OK" />
</form>
<p>
  <a href="/app/index">Back</a> |
  <a href="/app/show/1234">Show Foo</a>
</p>

Google App Engine