bert 1.0.1

Better Engine for Runtime Templates


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

bert - better engine for runtime templates

bert is an HTML-oriented run-time (instead of compile-time) templating engine implemented in D. Templates contain next to no logic (with surprisingly adept capabilities for checking existing variables).

Why run-time?

bert was built to support applications that require customizable templates, without recompiling the entire application. bert templates still parse and render very quickly and can be cached very easily.

Syntax

  • (( $variable )) -- variables are always prefixed with $, like in shell script or perl
  • ((? condition )), ((% else )) and ((% end )) for conditional blocks, for example, to check if variable "v" is set:
  ((? $v ))
     v is set
  ((% else ))
     v is not set
  ((% end ))
  • the else clause on conditionals can obviously be omitted
  • ((@ $needle; $haystack )) to iterate over an array, for example, to iterate over the 'users' array:
  ((@ $user; $users ))
     (( $user.name ))
  ((% end ))
  • ! before variable/expression to invert the truthiness of a value, !! just to convert it to a boolean
  • ((= $variable )) for a raw variable that doesn't escape (X|HT)ML characters
  • you can index into an array by accessing it like a member (as a workaround for not being able to access members.) for example, (( $array.0 )) functions as array[0] would in D. this also works for associative arrays!
  • anything outside double-parenthesis will be ignored and included as raw text in the template's output

Usage

D source code

module example;

import std.stdio : writefln;
import std.file : readText;

import bert.context;
import bert.structure;

void main() {
    // The Context holds variables for the template to reference.
    Context ctx = Context();
    ctx.set("sixty", "ten");
    ctx.set("user", [
        "name": "midna",
        "email": "[email protected]"
    ]);
    ctx.set("fruits", ["grape","orange","apple"]);

    // Structure is a fancy word for Template, but you can't name things "template" in D.
    Structure test = Structure(readText("test.bert"));
    writefln(test.render(ctx));
}

Bert template

(( $sixty ))     ((# => "ten" ))
(( $fruits.1 ))  ((# => "orange" ))
(( $user.name )) ((# => "midna" ))
((@ $fruit; $fruits ))
    (( $fruit ))
((% end ))       ((# => a list of all elements in 'fruits' ))

Reuse

bert is made available under the terms of the Lesser GNU General Public License, version 2.0

Authors:
Dependencies:
none
Versions:
1.1.1 2025-Jan-29
1.1.0 2025-Jan-29
1.0.4 2025-Jan-28
1.0.3 2025-Jan-28
1.0.1 2025-Jan-28
Show all 7 versions
Download Stats:
  • 0 downloads today

  • 16 downloads this week

  • 16 downloads this month

  • 16 downloads total

Score:
0.3
Short URL:
bert.dub.pm