thtml

HTML Templating Engine for TCL and twebserver

Contact: neophytos (at) gmail (dot) com

thtml git repo

Releases

  • The project is under active development. It has not been released yet.

Features

  • HTML with some additional tags to give it a dynamic flavor: foreach, if, include, val, and imports.
  • Supports a TCL-ish way for variable and dictionary substitution, and command execution.

Example

See sample-blog for a complete example using twebserver.

Supported Features

foreach

Template:

set template {
    <tpl foreach="item" in="${items}">
        <div class="item">
            <span class="name">${item.name}</span>
            <span class="value">${item.value}</span>
        </div>
    </tpl>
}

TCL:

::thtml::render $template {
    items {{
        name "item1"
        value "value1"
    }
    {
        name "item2"
        value "value2"
    }}
}

Expected output:

<div class="item">
    <span class="name">item1</span>
    <span class="value">value1</span>
</div>
<div class="item">
    <span class="name">item2</span>
    <span class="value">value2</span>
</div>

if

Template:

set template {
    <tpl if='$value eq "one"'>
        <div class="value1">Value is 1</div>
    </tpl>
    <tpl if='$value eq "two"'>
        <div class="value2">Value is 2</div>
    </tpl>
    <tpl if='$value eq "three"'>
        <div class="value3">Value is 3</div>
    </tpl>
}

TCL:

::thtml::render $template {value one}

Expected output:

<div class="value1">Value is 1</div>

include

Template:

set template {
    <tpl include="header.thtml" title="You are awesome!" />
    <div class="content">Content</div>
    <tpl include="footer.thtml" />
}

TCL:

::thtml::render $template {}

Expected output:

    <h1>You are awesome!</h1>
    <div class="content">Content</div>
    <strong>Footer</strong>

Where header.thtml is:

<h1>$title</h1>

And footer.thtml is:

<strong>Footer</strong>

val

Template:

set template {
    <html>
        <tpl val="x">return 1</tpl>
        <tpl val="y">return 2</tpl>
        <tpl val="z">return [expr { $x + $y }]</tpl>
        <head>
            <title>$title</title>
        </head>
        <body>
            <h1>$title</h1>
            <p>$z</p>
        </body>
    </html>
}

TCL:

::thtml::render $template {title "Hello World!"}