Skip to content

Commit

Permalink
Merge pull request #220 from haumacher/no-multiple-initializations
Browse files Browse the repository at this point in the history
Superfast baking with Thymeleaf
  • Loading branch information
jonbullock authored Jun 24, 2016
2 parents 809c197 + f5838ab commit 6e4a165
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main/java/org/jbake/template/ThymeleafTemplateEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,21 @@ public class ThymeleafTemplateEngine extends AbstractTemplateEngine {
private TemplateEngine templateEngine;
private FileTemplateResolver templateResolver;

private String templateMode;

public ThymeleafTemplateEngine(final CompositeConfiguration config, final ContentStore db, final File destination, final File templatesPath) {
super(config, db, destination, templatesPath);
initializeTemplateEngine();
}

private void initializeTemplateEngine() {
private void initializeTemplateEngine(String mode) {
if (mode.equals(templateMode)) {
return;
}
templateMode = mode;
templateResolver = new FileTemplateResolver();
templateResolver.setPrefix(templatesPath.getAbsolutePath() + File.separatorChar);
templateResolver.setCharacterEncoding(config.getString(Keys.TEMPLATE_ENCODING));
templateResolver.setTemplateMode(mode);
templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
try {
Expand All @@ -66,7 +72,6 @@ public void renderDocument(final Map<String, Object> model, final String templat
Context context = new Context(locale, wrap(model));
lock.lock();
try {
initializeTemplateEngine();
@SuppressWarnings("unchecked")
Map<String, Object> config = (Map<String, Object>) model.get("config");
@SuppressWarnings("unchecked")
Expand All @@ -79,7 +84,7 @@ public void renderDocument(final Map<String, Object> model, final String templat
mode = configMode;
}
}
templateResolver.setTemplateMode(mode);
initializeTemplateEngine(mode);
templateEngine.process(templateName, context, writer);
} finally {
lock.unlock();
Expand Down

0 comments on commit 6e4a165

Please sign in to comment.