Template Engine
The jhelm template engine (jhelm-gotemplate) is a Java implementation of Go’s text/template package.
It provides the core lexer, parser, AST, and executor pipeline.
1. Architecture
Template String → Lexer → Tokens → Parser → AST → Executor → Output
1.1. Modules
| Module | Purpose |
|---|---|
|
Core engine: lexer, parser, executor, AST nodes, |
|
Sprig function library (strings, collections, math, crypto, etc.) |
|
Helm function library (toYaml, include, lookup, etc.) |
2. Go Built-in Functions
These 19 functions are always available, matching Go’s text/template built-in set.
They live in Functions.GO_BUILTINS within jhelm-gotemplate.
2.1. Output
| Function | Signature | Description |
|---|---|---|
|
|
Concatenates arguments (like Go |
|
|
Formats string (like Go |
|
|
Joins arguments with spaces and appends newline |
2.2. Logic
| Function | Signature | Description |
|---|---|---|
|
|
Returns first falsy argument, or last argument if all truthy |
|
|
Returns first truthy argument, or last argument if all falsy |
|
|
Returns boolean negation of argument |
2.3. Comparison
| Function | Signature | Description |
|---|---|---|
|
|
Returns |
|
|
Returns |
|
|
Returns |
|
|
Returns |
|
|
Returns |
|
|
Returns |
2.4. Collection
| Function | Signature | Description |
|---|---|---|
|
|
Retrieves element from Map, List, or array by key/index |
|
|
Returns a sub-list/sub-array. |
|
|
Returns length of Collection, Map, String, or array |
3. Truthiness
Go templates use "truthiness" to evaluate values in conditionals (if, with, and, or).
jhelm implements the same rules:
-
null→ false -
Boolean→ its value -
String→ false if empty -
Number→ false if zero -
Collection→ false if empty -
Map→ false if empty -
Array→ false if length is zero -
Everything else → true
4. Usage
// Auto-discovery: finds Sprig + Helm providers on classpath
GoTemplate template = new GoTemplate();
template.parse("greeting", "Hello {{ .name }}!");
StringWriter writer = new StringWriter();
template.execute("greeting", Map.of("name", "World"), writer);
// writer.toString() == "Hello World!"
See Extending Functions for custom function providers.