In Apache FreeMarker, how can I get the absolute path to the directory containing the current .ftl
file?
For example, if I was processing the file /path/to/template.ftl
, then I'm searching for a way to get /path/to
inside of /path/to/template.ftl
.
I've tried .current_template_name
and friends, but these really only contain the name of the file, not its absolute path (from which I could get the parent directory). I've also tried absolute_template_name, but this just seems to prepend the name with a /
to make the path seem absolute, but it does not resolve to the real absolute path.
Background: I'm templatizing Asciidoc files with with Freemarker, and the Asciidoc files must include other Asciidoc files which reside below the original directory of the .flt
file, so they must not be searched relative to the temporarily "expanded" Asciidoc file.
The template paths that templates use are always virtual, and resolved by the TemplateLoader
object set in the Configuration
. TemplateLoader
is just an interface, has multiple implementations, and so is a black box for FreeMarker. The actual location of the template can be inside a jar file, or even in the database table, so in general a template has no path on the file system.
Normally, you set up the TemplateLoader
so that it can access all the templates you need. Then you don't need any tricks, and just use template paths.
Another possibility is to use a FileTemplateLoader
that uses the root directory as base directory. This of course would be a bad idea for most applications (especially for web applications, for security reasons).