Logo

index : htmltemplate

---

  • summary
  • about
  • tree
  • log
  • branches
https://git.ptrace.dev/public/htmltemplate.git
ssh://git@git.ptrace.dev/public/htmltemplate.git
JAI: 89%
MD: 11%

htmltemplate

A tiny html template 'engine'. It currently supports:

  • replacements of values. Via numbered or not-numbered parameters
  • loops inside the template file

Example

If you want an example with more explainations, consult examples/02_with_comments.jai.

If you want to know what you else can do with the template syntax, look at the unit tests & comments in test/test.jai.

HTML template:

<div>
    <p>{{ :foo1: }}</p>

    {{ :foo2: <a href="%" %>%</a> }}

    {{ :foo3: <a href="#%1" target="%2">%1</a> }}

    <ul>
{{ loop:foo4: <li><a href="#%1">%1</a>%2</li> }}
    </ul>
</div>

Code:

#import "Basic";
#import "htmltemplate";


main :: () {
    queue_action: [..]Action;
    defer array_free(queue_action);

    commit("foo1", "My Title");
    commit("foo2", "#about", "target=\"_self\"", "About");
    commit("foo3", "blog", "_self");
    commit("foo4", .[
        .["Foo", "Bar"],
        .["Fizz", "Buzz"],
        .["contact", ""],
    ]);

    success,
    html_string,
    exit_code,
    error_message := generate(queue_action, TEMPLATE, .STRING);

    defer {
        free(html_string);
        free(error_message);
    }

    if !success {
        log("%", error_message);
        return;
    }

    log("%", html_string);
}

Output:

<div>
    <p>My Title</p>

    <a href="#about" target="_self">About</a>

    <a href="#blog" target="_self">blog</a>

    <ul>
<li><a href="#Foo">Foo</a>Bar</li>
<li><a href="#Fizz">Fizz</a>Buzz</li>
<li><a href="#contact">contact</a></li>
    </ul>
</div>

Compiler Version

$ jai -version
Version: beta 0.2.028, built on 9 April 2026.

Usage

Put the library into your project-local modules dir, or where ever you defined your library path.

Tests

You can run unit tests with: jai build.jai - run silent.

BNF

statement      =  "{{" ( replace | loop ) "}}" ;

replace        =  ":" identifier ":" ( html_code )? ;
loop           =  "loop" ":" identifier ":" html_code ;

html_code      =  { UNICODE | replace_marks | escape_marks } ;
replace_marks  =  "%" | "%" number ;

escape_marks   =  "\%" ;
identifier     =  UNICODE ;
number         =  [1-9] ;
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit