fponticelli edited this page Sep 13, 2010 · 4 revisions

htemplate is a simple but powerfull template system for haXe. It is cross-platform and uses hscript to perform the template logic.
htemplate is still very young, so be warned that changes can still occur in both API and template syntax.

Getting Started

haXe Code

To use the template system just do the following:

class Main {
  static function main() {
    var s = "Hello {:name}!";
    var template = new htemplate.Template(s);
    trace(template.execute({ name : "haXe" }));
  }
}

Template Syntax

print

{: name}

Prints the value of name; to be exact it prints out the value generated by the code block.

code block

{eval}title = "title: " + name.toUpperCase();{end}

Executes the code block without printing anything directly.
Alternative syntax:

{? title = "title: " + name.toUpperCase();}

logic block

{if(name != null)}My name is {: name}{else}What is my name?{end}

Defines a logic block. Logic blocks are available for if, else if, else, for and while. Logic blocks must always be terminated by {end}.

set from content

{set pagination}page: 1, 2, 3{end}

Captures the content between {set …} and {end} and assign the value to the variable named pagination.

example

A more advanced example, the template:

{eval}
  ucwords = function(s) {
    var r = '';
    var arr = s.split(' ');
    for (v in arr)
      r += v.substr(0, 1).toUpperCase() + v.substr(1, null) + ' ';
    return r;
  };

  list.sort(function(a, b) {
    if (a.name == b.name)
      return 0;
    else if (a.name < b.name)
      return -1
    else
      1;
  });
{end}
{set line}{:repeat('=', title.length)}{end}

{: line}
{: ucwords(title)}
{: line }

{: content.substr(0, 40)}...

{for item in list}
  * {if(item.sex == 'f')}Ms.{else}Mr.{end} {: item.name}
{end}

… and the code:

var h = new Hash<Dynamic>();
h.set('title', 'htemplate - the template system for haxe');
h.set('content', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ullamcorper felis non libero blandit facilisis. In hac habitasse platea dictumst');
h.set('list', [
  { sex : 'm', name : 'Boris' },
  { sex : 'f', name : 'Doris' },
  { sex : 'm', name : 'John' },
  { sex : 'f', name : 'Jane' },
]);
h.set('repeat', function(v, l) {
  return StringTools.lpad('', v, l);
});

var template = new htemplate.Template(TEMPLATE);
trace(template.execute(h));
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.