Kwartzの学習メモページ

Kwartz - kuwata-lab.com
るびま
るびま

学習したことをメモがてら、載せていく。

ex

プレゼンテーションデータファイルex.html

<table>

  <tr id="list">
    <td id="mark:item">Foo</td>
  </tr>

  <tr id="dummy:d1">
    <td>Bar</td>
  </tr>

</table>

プレゼンテーションロジックファイルex.plogic

#list {
  logic: {
    for member in @members
      _stag
      _cont
      _etag
    end
  } 
} 

#item {
  value: @x;
}


プレゼンテーションデータファイルとプレゼンテーションロジックファイルから、
テンプレートファイルex.rbhtmlを生成する。

$ kwartz -l rbtenjin -p ex.plogic ex.html > ex.rbhtml


テンプレートファイルex.rbhtml

<table>

<?rb     for member in @members ?>
  <tr id="list">
    <td>#{@x}</td>
  </tr>
<?rb     end ?>


</table>


メインプログラムファイル

#!/home/rdera/.rvm/rubies/ruby-1.9.2-p180/bin/ruby -Ku

require 'tenjin'

engine = Tenjin::Engine.new()

context = { 
  :members => ['A','B','C'],
  :x       => 'abcdefg',
}

s = engine.render('ex.rbhtml',context)
print s


出力は以下の通り。

<table>

  <tr id="list">
    <td>abcdefg</td>
  </tr>
  <tr id="list">
    <td>abcdefg</td>
  </tr>
  <tr id="list">
    <td>abcdefg</td>
  </tr>


</table>


Kwartz-Ruby Users' Guide - kuwata-lab.com
沿って、学習を進める。

まず、『1−4Complex Example』

1-4

プレゼンテーションファイルexample2.html

<table>

 <tr bgcolor="#CCCCFF" id="mark:list">
  <td id="mark:name">foo</td>
  <td>
   <a href="mailto:foo@mail.com" id="mark:email">foo@mail.com</a>
  </td>
 </tr>


</table>

プレゼンテーションロジックexample2.plogic

/*
 * an element which is marked by 'id="mark:list"'
 *  - print value of a variable 'color' as bgcolor attribute value.
 */
#list {
  attrs:  "bgcolor" color;
  logic: {
    @members.each_with_index do |member, i|
      color = i % 2 == 0 ? '#FFCCCC' : '#CCCCFF';
      _stag    # start tag
      _cont    # content
      _etag    # end tag
    end
  }
}

/*
 * an element which is marked by 'id="mark:name"':
 *  - print value of member[:name] as content of the element.
 */
#name {
  value: member[:name];
}

/*
 * an element marked by 'id="mark:email"':
 *  - print value of member[:email] as contentn of the element. 
 *  - print "mailto:" and member[:email] as href attribute value.
 */
#email {
  value: member[:email];
  attrs: "href" "mailto:#{member[:email]}";
}

テンプレートファイルを生成する。

$ kwartz -l rbtenjin -p example2.plogic example2.html > example2.rbhtml


テンプレートファイルexample2.rbhtmlは以下の通り。

<table>
 
<?rb     @members.each_with_index do |member, i| ?>
<?rb       color = i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'; ?>
 <tr bgcolor="#{color}">
  <td>#{member[:name]}</td>
  <td>
   <a href="#{"mailto:#{member[:email]}"}">#{member[:email]}</a>
  </td>
 </tr>
<?rb     end ?>
      
    
</table>


メインプログラム

#!/home/rdera/.rvm/rubies/ruby-1.9.2-p180/bin/ruby -Ku

require 'tenjin'

engine = Tenjin::Engine.new()

context = { 
  :members => ['x','y'],
  :name    => 'name',
  :email   => 'email',
  :x       => { 'name' => 'xxx', 'email' => 'em'},
  :y       => { 'name' => 'yyy', 'email' => 'em'}
}

s = engine.render('example2.rbhtml',context)
print s


実行してみたが、うまくいかない。
以下、実行時のメッセージ。

example2.rbhtml:6:in `[]': can't convert Symbol into Integer (TypeError)
        from example2.rbhtml:6:in `block (2 levels) in _render'
        from example2.rbhtml:3:in `each'
        from example2.rbhtml:3:in `each_with_index'
        from example2.rbhtml:3:in `block in _render'
        from /home/rdera/.rvm/gems/ruby-1.9.2-p180/gems/tenjin-0.6.1/lib/tenjin.rb:625:in `instance_eval'
        from /home/rdera/.rvm/gems/ruby-1.9.2-p180/gems/tenjin-0.6.1/lib/tenjin.rb:625:in `render'
        from /home/rdera/.rvm/gems/ruby-1.9.2-p180/gems/tenjin-0.6.1/lib/tenjin.rb:940:in `render'
        from ./tenjin2.rb:15:in `<main>'

よく分からず、ハマっている。。。
とりあえず、飛ばして、学習を進める。

1-5

どのようなプレゼンテーションロジックを書けば、
どのようなテンプレートファイルが生成されるのか。

プレゼンテーションデータファイルは引き続き、以下の通り。

<table>

 <tr bgcolor="#CCCCFF" id="mark:list">
  <td id="mark:name">foo</td>
  <td>
   <a href="mailto:foo@mail.com" id="mark:email">foo@mail.com</a>
  </td>
 </tr>

</table>

プレゼンテーションロジックファイル

#list {
  logic: {
    for member in @members
      _stag
      _cont
      _etag
    end 
  }
}

テンプレート

<table>

<?rb     for member in @members ?>
 <tr bgcolor="#CCCCFF">
  <td>foo</td>
  <td>
   <a href="mailto:foo@mail.com">foo@mail.com</a>
  </td>
 </tr>
<?rb     end ?>

</table>

2

プレゼンテーションロジックファイル

#list {
  logic: {
    for member in @members
      _elem
    end 
  }
}

テンプレートは1と同様。

3

プレゼンテーションロジックファイル

#list {
  logic: {
    _stag
    for member in @members
      _cont
    end 
    _etag
  }
}

テンプレート

<table>

 <tr bgcolor="#CCCCFF">
<?rb     for member in @members ?>
  <td>foo</td>
  <td>
   <a href="mailto:foo@mail.com">foo@mail.com</a>
  </td>
<?rb     end ?>
 </tr>

</table>

4

プレゼンテーションロジックファイル

#list {
  logic: {
    _stag
    print expr
    _etag
  }
}

テンプレート

<table>

 <tr bgcolor="#CCCCFF">
#{expr} </tr>

</table>

5

プレゼンテーションロジックファイル

#list {
    value: expr;
}

テンプレートは4と同様。

6

プレゼンテーションロジックファイル

#list {
  logic: {
    print expr
  }
}

テンプレート

<table>

#{expr}
</table>

7

プレゼンテーションロジックファイル

#list {
  elem: expr;
}

テンプレートは6と同様。

8

>プレゼンテーションロジックファイル

#list {
  logic: {
    _cont
  }
}

テンプレート

<table>

  <td>foo</td>
  <td>
   <a href="mailto:foo@mail.com">foo@mail.com</a>
  </td>

</table>

9

プレゼンテーションロジックファイル

#list {
  logic: {
  }
}

テンプレート

<table>


</table>

10

プレゼンテーションロジックファイル

#list {
  logic: {
    _element(foo)  # expand other element marked with name 'foo'
  }
} 

テンプレート
.....
いまいち、良く分からず。
保留。

11

プレゼンテーションロジックファイル

#list {
  logic: {
    _content(foo)  # expand content of other element
  }
}

テンプレート
.....
いまいち、良く分からず。
保留。

12

プレゼンテーションロジックファイル

#list {
  logic: {
    i = 0
    for member in @members
      i += 1
      if i % 2 == 0
        color = '#FFCCCC'
      else
        color = '#CCCCFF'
      end
      _elem    # '_elem' is equivarent to _stag + _cont + _etag
    end
  }
}

テンプレート

<table>

<?rb     i = 0 ?>
<?rb     for member in @members ?>
<?rb       i += 1 ?>
<?rb       if i % 2 == 0 ?>
<?rb         color = '#FFCCCC' ?>
<?rb       else ?>
<?rb         color = '#CCCCFF' ?>
<?rb       end ?>
 <tr bgcolor="#CCCCFF">
  <td>foo</td>
  <td>
   <a href="mailto:foo@mail.com">foo@mail.com</a>
  </td>
 </tr>
<?rb     end ?>

</table>