The runtime environment of templates is represented by the Environment
object, which you also get as the parameter of TemplateDirectiveModel
-s, or can get from TLS with the static method Environment.getCurrentEnvironment()
. You can attach additional objects, like the session, to the Environment with setCustomAttribute(String name, Object value)
and so your directives can get it with env.getCustomAttribute(name)
.
Each Template.process
call makes its own Environment
object internally. To access it before the main template is called, you have to do this instead of simply calling Tempalte.process
:
Environment env = myTemplate.createProcessingEnvironment(root, out);
env.setCustoomAttribute("session", sesssion);
// You can addjust other environment settings here too.
env.process(); // template is called here to render the output
Another possibility is putting the session into the data-model. TemplateDirective
-s can access the data-model via env.getDataModel()
.