Configure Template Loading with ResourceLocators
masterJinjava uses ResourceLocator implementations to resolve template paths (e.g., for {% extends %} or {% include %}).
- Default Behavior: Uses
ClasspathResourceLocator, which loads files from the classpath. - File System Access: Use
FileResourceLocatorto load files from the file system. Warning: This carries security risks if user input is used to define paths, as it could allow unauthorized file access (e.g.,{% include '/etc/password' %}). - Custom Loading: Implement the
ResourceLoaderinterface to hook into your own application's template repository. - Multiple Locators: Use
CascadingResourceLocatorto search through multiple locations sequentially.
To set a locator:
JinjavaConfig config = JinjavaConfig.builder().build();
Jinjava jinjava = new Jinjava(config);
// Set a single custom locator
jinjava.setResourceLocator(new MyCustomResourceLocator());
// Set multiple locators (cascading)
jinjava.setResourceLocator(new MyCustomResourceLocator(), new FileResourceLocator());