Currently, MustacheAutoConfiguration compiler only support two options, loader and collector. Would you please add more options for convenience, e.g. defaultValue/nullValue.
public Mustache.Compiler mustacheCompiler(TemplateLoader mustacheTemplateLoader) {
return Mustache.compiler().withLoader(mustacheTemplateLoader)
.withCollector(collector());
}
Comment From: snicoll
e.g. defaultValue/nullValue.
Thanks for the suggestion. We can improve the auto-configuration but I'd rather do that with concrete use cases rather than based on a couple suggestions. Do you have some concrete use case or some customizations you've applied in your own project you'd like to share?
Comment From: fjcanyue
In my own project, if I use default compiler, an exception will be thrown when template variable value is null. (e.g. one variable is an empty object, {{object.field}} will throw an exception)
So I have to create a customized configuration to avoid that, but not use MustacheEnvironmentCollector.
@Configuration
public class MustacheAutoConfiguration
extends org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration {
public MustacheAutoConfiguration(MustacheProperties mustache, Environment environment,
ApplicationContext applicationContext) {
super(mustache, environment, applicationContext);
}
@Override
public Compiler mustacheCompiler(TemplateLoader mustacheTemplateLoader) {
return Mustache.compiler().defaultValue("").withLoader(mustacheTemplateLoader);
}
}
Comment From: snicoll
@fjcanyue Thanks for the feedback. FYI, auto-configurations are not meant to be extended that way. There is a @ConditionalOnMissingBean on the Compiler so all that code is unnecessary, really. All you need is to create a Compiler @Bean and inject the auto-configured TemplateLoader:
@Configuration
public class MyUserConfig {
@Bean
public Compiler mustacheCompiler(TemplateLoader mustacheTemplateLoader) {
return Mustache.compiler().defaultValue("").withLoader(mustacheTemplateLoader);
}
}
I'd be tempted to add a CompilerCustomizer so that you can register that only instead (i.e. keep the default values and just add your own).
Let's see what the rest of the team thinks.
Comment From: wilkinsona
I think it's worth doing something here. Recreating the existing Compiler configuration is a little bit involved due to the MustacheEnvironmentCollector. Perhaps it should be a bean? I think there's a case for some configuration properties (standardsMode, defaultValue, etc), some additional optional beans (Escaper, Formatter), and perhaps a customizer too. I'm not yet sure which combination of the three would be best.