Skip to content

Commit 91d6e2f

Browse files
committed
Improve extensibility and reusability of MustacheTemplateEngine
Previously, MustacheTemplateEngine hard coded the configuration of its Mustache Compiler. This commit adds an overloaded constructor that allows a custom Compiler to be used. It also adds protected accessors for the compiler and the template resource resolver so that they can be easily accessed by subclasses. Closes gh-149
1 parent 2410f06 commit 91d6e2f

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/mustache/MustacheTemplateEngine.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,18 +36,32 @@
3636
*/
3737
public class MustacheTemplateEngine implements TemplateEngine {
3838

39-
private final Compiler compiler = Mustache.compiler().escapeHTML(false);
39+
private final Compiler compiler;
4040

4141
private final TemplateResourceResolver templateResourceResolver;
4242

4343
/**
44-
* Creates a new {@link MustacheTemplateEngine} that will use the given
44+
* Creates a new {@code MustacheTemplateEngine} that will use the given
4545
* {@code templateResourceResolver} to resolve template paths.
4646
*
47-
* @param templateResourceResolver The resolve to use
47+
* @param templateResourceResolver the resolver to use
4848
*/
4949
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver) {
50+
this(templateResourceResolver, Mustache.compiler().escapeHTML(false));
51+
}
52+
53+
/**
54+
* Creates a new {@code MustacheTemplateEngine} that will use the given
55+
* {@code tempalteResourceResolver} to resolve templates and the given
56+
* {@code compiler} to compile them.
57+
*
58+
* @param templateResourceResolver the resolver to use
59+
* @param compiler the compiler to use
60+
*/
61+
public MustacheTemplateEngine(TemplateResourceResolver templateResourceResolver,
62+
Compiler compiler) {
5063
this.templateResourceResolver = templateResourceResolver;
64+
this.compiler = compiler;
5165
}
5266

5367
@Override
@@ -58,4 +72,23 @@ public Template compileTemplate(String name) throws IOException {
5872
templateResource.getInputStream())));
5973
}
6074

75+
/**
76+
* Returns the {@link Compiler} used to compile Mustache templates.
77+
*
78+
* @return the compiler
79+
*/
80+
protected final Compiler getCompiler() {
81+
return this.compiler;
82+
}
83+
84+
/**
85+
* Returns the {@link TemplateResourceResolver} used to resolve the template resources
86+
* prior to compilation.
87+
*
88+
* @return the resolver
89+
*/
90+
protected final TemplateResourceResolver getTemplateResourceResolver() {
91+
return this.templateResourceResolver;
92+
}
93+
6194
}

0 commit comments

Comments
 (0)