-
Notifications
You must be signed in to change notification settings - Fork 0
XTML Module Development Guide
Andreas Wagner edited this page Nov 16, 2025
·
1 revision
Modules in XTML allow you to extend the language with custom functions that can be used inside <xtml> blocks. Modules are implemented as C++ shared libraries (DLLs) and can be loaded at runtime.
Every XTML module must implement a class that inherits from the base Module class:
#include "FunctionRegistry.h"
class MyModule : public Module {
public:
void RegisterFunctions(FunctionRegistry& registry) override {
// Register your functions here
}
~MyModule() = default;
};-
RegisterFunctionsis called by XTML to register all functions provided by your module. - You can define functions in namespaces to organize them logically.
Use FunctionRegistry to expose functions to XTML templates:
registry.RegisterNamespace("mymodule"); // namespace first
registry.RegisterFunction("mymodule", "add", [](const std::vector<var>& args) -> var {
if (args.size() != 2 || args[0].type != DT_NUMBER || args[1].type != DT_NUMBER) {
Utils::printerrLn("Error: add expects 2 numeric arguments.");
return var{"", DT_UNKNOWN};
}
int sum = std::stoi(args[0].value) + std::stoi(args[1].value);
return var{std::to_string(sum), DT_NUMBER};
}, 2, 2);- Arguments:
argsis a vector ofvarobjects. - Return value: return a
varobject representing the function result. - Min and max arguments: define how many arguments are required.
- Create a DLL project in Visual Studio (or your preferred C++ toolchain).
- Include
FunctionRegistry.hand link against XTML core library if needed. - Implement your module class as shown above.
- Export the module class in a factory function:
extern "C" __declspec(dllexport) Module* CreateModule() {
return new MyModule();
}- Build the DLL and place it in the
modulesfolder of XTML (get created during the first start of xtml).
Once a module is loaded, its functions can be called inside <xtml> blocks:
<xtml>
var result = native mymodule::add(10, 5); // returns 15
print("Sum: " + result);
</xtml>
- Functions must be namespaced with the
nativekeyword (e.g., native mymodule::add).
- Ensure functions handle incorrect argument types and count gracefully.
- Return meaningful error messages via
Utils::printerrLn. - Modules can provide any type of value (
DT_NUMBER,DT_STRING,DT_ARRAY,DT_BOOL). - Test modules thoroughly before release.
Modules make XTML highly extensible and allow you to integrate custom C++ functionality directly into templates.