Last Modified
2012-04-28 17:00:54 +0000
Requires
  • htree/parse
  • htree/gencode
  • htree/equality
  • htree/traverse

Description

Template Engine

The htree template engine converts HTML and some data to HTML or XML.

Template Method Summary

Note that the following method, HTree(), is not a template method.

Template Directives.

A template directive is described as a special HTML attribute which name begins with underscore.

The template directives are listed as follows.

Template Semantics

White Space Handling

The htree template engine strips whitespace text nodes in a template except under HTML pre element.

For example the white space text node between two spans in following template is stripped.

<span _text="'a'"/> <span _text="'b'"/> -> "ab"

Character entity references are not stripped.

<span _text="'a'"/>&#32;<span _text="'b'"/> -> "a&#32;b"

Text nodes generated by _text is not stripped.

<span _text="'a'"/><span _text="' '"> </span><span _text="'b'"/> -> "a b"

HTML and XML

The htree template engine outputs HTML or XML.

If a template has no XML declaration and the top element is HTML, the result is HTML. Otherwise the result is XML.

They differs as follows.

Design Decision on Design/Logic Separation

HTree template engine doesn't force you to separate design and logic. Any logic (Ruby code) can be embedded in design (HTML).

However the template engine cares the separation by logic refactorings. The logic is easy to move between a template and an application. For example, following tangled template

tmpl.html:
  <html>
    <head>
      <title _text="very-complex-ruby-code">dummy</title>
    </head>
    ...
  </html>

app.rb:
  HTree.expand_template('tmpl.html', obj)

can be refactored as follows.

tmpl.html:
  <html>
    <head>
      <title _text="title">dummy</title>
    </head>
    ...
  </html>

app.rb:
  def obj.title
    very-complex-ruby-code
  end
  HTree.expand_template('tmpl.html', obj)

In general, any expression in a template can be refactored to an application by extracting it as a method. In JSP, this is difficult especially for a code fragment of an iteration.

Also HTree encourages to separate business logic (Ruby code in an application) and presentation logic (Ruby code in a template). For example, presentation logic to color table rows stripe can be embedded in a template. It doesn't need to tangle an application.