I need to use JinJava to process small templates that can contain different tokens. For example:
Hello,{{ user }}...
where a binding user
is required because without it the output does not make any sense.
My class does not know what tokens template uses and it gets a list of bindings from remote services.
Therefore, there may be a case when a binding that is specified in a template does not exist. JinJava's default behavior is to use null
that is converted to empty string in the output string. It does not work in my case, I need an exception to be thrown if any of bindings is missed.
My current solution is to use a custom Filter
/**
* A filter for JinJava that verifies that a value is not null and if the value is null, writes a fatal error to interpreter's log
*/
public class RequiredFilter implements Filter {
@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
if (var == null) {
interpreter.addError(new TemplateError(TemplateError.ErrorType.FATAL, TemplateError.ErrorReason.MISSING, "Value of a required key is null", "", 1, null));
}
return var;
}
@Override
public String getName() {
return "required";
}
}
And make clients to explicitly specify this function for all tokens in templates. The example above now looks like:
Hello, {{ user|required }}...
and if the binding user
does not exist, I get my exception. However, the requirement of a custom function looks strange.
Is there a way to set up JinJava to achieve the similar behavior and do not bother client by the requirement to specify the filter required
every time?