bert 1.1.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.
Features
(( $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 %)
- Iterator blocks will set
$FIRST
or$LAST
on the first and last items respectively, but won't anywhere else (useful for comma-separating a list) !
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(^ $variable (or "string") ^)
to include a template initialized by a loader- 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 asarray[0]
would in D. this also works for associative arrays (e.g.(( $array.key ))
evaluating toarray["key"]
) - anything outside double-parenthesis will be ignored and included as raw text in the template's output
Usage
Example
source/app.d
:
module app;
import std.stdio : writefln;
import std.file : readText;
import bert.context;
import bert.structure;
import bert.loader;
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"]);
// Loaders load templates in a fashion that allows them to include others. It also provides
// a prettier API :)
Loader loader = new Loader("./templates");
writefln("%s", loader.render("test", ctx));
}
templates/test.bert
:
(( $sixty )) (# => "ten" #)
(( $fruits.1 )) (# => "orange" #)
(( $user.name )) (# => "midna" #)
(# a comma-separated, weirdly-spaced list of all elements in 'fruits' #)
(@ $fruit; $fruits @)(( $fruit ))(? !$LAST ?), (% end %)(% end %)
Reuse
bert is made available under the terms of the Lesser GNU General Public License, version 2.0
- Registered by midna w.
- 1.1.1 released 3 days ago
- dax-ssg/bert
- LGPL-3.0-only
- Copyright © 2025, midna w.
- 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 - Download Stats:
-
-
0 downloads today
-
16 downloads this week
-
16 downloads this month
-
16 downloads total
-
- Score:
- 0.3
- Short URL:
- bert.dub.pm