The JHP API consists of several core classes that work together to provide template rendering functionality.
The main class for rendering templates.
com.hindbiswas.jhp.engine.JhpEnginepublic JhpEngine(Settings settings, FunctionLibraryContext functionLibrary)Parameters:
settings- Configuration settings for the enginefunctionLibrary- Function library instance (typicallyFunctionLibrary)
Example:
Settings settings = Settings.builder().base("/templates").build();
FunctionLibrary lib = new FunctionLibrary();
JhpEngine engine = new JhpEngine(settings, lib);public String render(Path templatePath, Context context) throws ExceptionRenders a template using a Path object.
Parameters:
templatePath- Absolute path to the template filecontext- Context containing variables
Returns: Rendered template as String
Throws: Exception if template parsing or rendering fails
Example:
Path path = Path.of("/templates/page.jhp");
Context ctx = new Context();
ctx.add("title", "My Page");
String output = engine.render(path, ctx);public String render(String pathTxt, Context context) throws ExceptionRenders a template using a string path (relative to base directory).
Parameters:
pathTxt- Path to template (relative to base directory)context- Context containing variables
Returns: Rendered template as String
Throws: Exception if template parsing or rendering fails
Example:
Context ctx = new Context();
ctx.add("title", "My Page");
String output = engine.render("page.jhp", ctx);JhpEngine is thread-safe and can be shared across multiple threads.
Holds variables that will be available in templates.
com.hindbiswas.jhp.Contextpublic Context()Creates a new empty context.
Example:
Context ctx = new Context();public void add(String key, Object value)Adds a variable to the context.
Parameters:
key- Variable namevalue- Variable value (any Object)
Example:
ctx.add("name", "Alice");
ctx.add("age", 25);
ctx.add("items", Arrays.asList("A", "B", "C"));public Map<String, Object> getContext()Returns the underlying map of context variables.
Returns: Map containing all context variables
Example:
Map<String, Object> data = ctx.getContext();
System.out.println(data.get("name"));Context automatically converts values:
- Primitives and Strings - Stored as-is
- Collections - Converted to List
- Arrays - Converted to Object[]
- Maps - Stored as-is
- POJOs - Converted to Map (public fields only)
Configuration for the JHP engine.
com.hindbiswas.jhp.engine.Settingspublic static SettingsBuilder builder()Creates a new settings builder.
Returns: SettingsBuilder instance
Example:
Settings settings = Settings.builder()
.base("/templates")
.build();public final Path baseBase directory for templates.
public final boolean escapeByDefaultWhether to HTML-escape output by default.
public final int maxIncludeDepthMaximum depth for nested includes.
public final IssueHandleMode issueHandleModeHow to handle runtime issues.
public static int defaultMaxIncludeDepth = 15Default maximum include depth.
public static boolean defaultEscapeMode = trueDefault HTML escaping mode.
public static IssueHandleMode defaultIssueHandleMode = IssueHandleMode.COMMENTDefault issue handling mode.
Builder for creating Settings instances.
com.hindbiswas.jhp.engine.Settings.SettingsBuilderpublic SettingsBuilder base(String base)Sets the base directory.
Parameters:
base- Path to base directory
Returns: This builder
public SettingsBuilder escape(boolean escapeByDefault)Sets HTML escaping mode.
Parameters:
escapeByDefault- true to enable escaping
Returns: This builder
public SettingsBuilder maxIncludeDepth(int maxIncludeDepth)Sets maximum include depth.
Parameters:
maxIncludeDepth- Maximum depth
Returns: This builder
public SettingsBuilder issueHandleMode(IssueHandleMode issueHandleMode)Sets issue handling mode.
Parameters:
issueHandleMode- Issue handling mode
Returns: This builder
public Settings build()Builds the Settings instance.
Returns: Immutable Settings object
Manages built-in and custom functions.
com.hindbiswas.jhp.engine.FunctionLibrarypublic FunctionLibrary()Creates a new function library with built-in functions.
public JhpFunction register(String name, JhpFunction fn) throws IllegalArgumentExceptionRegisters a custom function.
Parameters:
name- Function name (case-insensitive)fn- Function implementation
Returns: Previously registered function with same name, or null
Throws: IllegalArgumentException if name or function is null/empty
Example:
lib.register("double", (args, scopes) -> {
Number n = (Number) args.get(0);
return n.doubleValue() * 2;
});public JhpFunction unregister(String name)Unregisters a custom function.
Parameters:
name- Function name
Returns: Unregistered function, or null
public boolean hasFunction(String name)Checks if a function exists.
Parameters:
name- Function name
Returns: true if function exists
callFunction(String, List, Deque<Map<String, Object>>)
public Object callFunction(String name, List<Object> args, Deque<Map<String, Object>> scopes)
public Object callFunction(String name, List<Object> args, Deque<Map<String, Object>> scopes)Calls a function (used internally by the engine).
Parameters:
name- Function nameargs- Function argumentsscopes- Variable scopes
Returns: Function result
Throws: IllegalArgumentException if function not found
JhpFunction
Functional interface for custom functions.
Package
com.hindbiswas.jhp.engine.FunctionLibrary.JhpFunctionMethod
Object apply(List<Object> args, Deque<Map<String, Object>> scopes)Parameters:
args- List of arguments passed to functionscopes- Stack of variable scopes (for accessing context)
Returns: Function result (any Object)
Example:
JhpFunction myFunc = (args, scopes) -> {
if (args.isEmpty()) return "";
return args.get(0).toString().toUpperCase();
};Enums
IssueHandleMode
How the engine handles runtime issues.
Package: com.hindbiswas.jhp.engine.IssueHandleMode
Values:
DEBUG- Render detailed debug informationTHROW- Throw exceptionsCOMMENT- Render as HTML commentsIGNORE- Silently ignore
Example:
Settings settings = Settings.builder()
.issueHandleMode(IssueHandleMode.THROW)
.build();IssueType
Types of runtime issues.
Package: com.hindbiswas.jhp.engine.IssueType
Values:
INCLUDE_CYCLE- Circular include detectedMISSING_VARIABLE- Variable not foundFUNCTION_ERROR- Function execution errorEXPRESSION_ERROR- Expression evaluation errorMISSING_INCLUDE- Include file not foundINCLUDE_NOT_IN_BASE_DIR- Include outside base directoryINCLUDE_MAX_DEPTH- Max include depth exceededINCLUDE_ERROR- General include errorUNKNOWN_ELEMENT- Unknown template elementFUNCTION_CALL_ERROR- Invalid function call
Exceptions
PathNotInBaseDirectoryException
Thrown when an include path is outside the base directory.
Package: com.hindbiswas.jhp.errors.PathNotInBaseDirectoryException
Constructor:
public PathNotInBaseDirectoryException(Path path, Path baseDirectory)InvalidFileTypeException
Thrown when a file has an invalid type.
Package: com.hindbiswas.jhp.errors.InvalidFileTypeException
Interfaces
FunctionLibraryContext
Interface for function libraries.
Package: com.hindbiswas.jhp.engine.FunctionLibraryContext
Method:
Object callFunction(String name, List<Object> args, Deque<Map<String, Object>> scopes)PathResolver
Interface for resolving template paths.
Package: com.hindbiswas.jhp.engine.PathResolver
Method:
Path resolve(String raw, Path includingFileDir) throws PathNotInBaseDirectoryExceptionPathToAstParser
Interface for parsing templates to AST.
Package: com.hindbiswas.jhp.engine.PathToAstParser
Method:
TemplateNode parse(Path path) throws ExceptionRuntimeIssueResolver
Interface for handling runtime issues.
Package: com.hindbiswas.jhp.engine.RuntimeIssueResolver
Method:
void handle(IssueType type, String message, StringBuilder sb, ThreadLocal<Deque<Path>> includeStack)Built-in Functions Reference
String Functions
| Function | Description | Example |
|---|---|---|
touppercase(str) |
Convert to uppercase | {{ touppercase("hello") }} → HELLO |
tolowercase(str) |
Convert to lowercase | {{ tolowercase("HELLO") }} → hello |
trim(str) |
Remove whitespace | {{ trim(" hi ") }} → hi |
Utility Functions
| Function | Description | Example |
|---|---|---|
len(value) |
Get length/size | {{ len("hello") }} → 5 |
now() |
Current timestamp | {{ now() }} → 2025-10-05T00:18:03Z |
Next Steps
- Check out Examples
- Review Advanced Usage
- Explore the source code