title: Process single Thymeleaf fragment with Spring Boot
body: <p>In my Spring Boot application, I need to process a single Thymeleaf Fragment to get its rendered HTML output.</p>
  
  <p>I'm injecting</p>
  
  <pre><code>@Autowired
  private SpringTemplateEngine templateEngine;
  </code></pre>
  
  <p>and then try to</p>
  
  <pre><code>Context context = new Context();
  context.setVariable("key", value);
  
  String html = this.templateEngine.process("fragments/foo :: bar(key=${key})", context);
  </code></pre>
  
  <p>with <code>foo.html</code> located in <code>src/main/resources/templates/fragments</code>:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
  ...
  
  &lt;th:block th:fragment="bar(key)"&gt;
      &lt;!-- doing fancy stuff with key... --&gt;
  &lt;/th:block&gt;
  
  ...
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I didn't change any autoconfigured values of Thymeleaf.</p>
  
  <p>Running this code, I get the following exception:</p>
  
  <pre><code>org.thymeleaf.exceptions.TemplateInputException: Error resolving template "fragments/foo :: bar(key=${key})", template might not exist or might not be accessible by any of the configured Template Resolvers
      at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:924) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:898) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
      ...
  </code></pre>
  
  <p>Am I doing anything wrong here? The whole stuff gets working if I create a file <code>src/main/resources/templates/fragments/test.html</code> with simply the content</p>
  
  <pre><code>&lt;div th:replace="fragments/foo :: bar(key=${key})"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>and process it via</p>
  
  <pre><code>String html = this.templateEngine.process("fragments/test", context);
  </code></pre>
  
  <p>That would be a possible solution, but seems a bit hackish to me...</p>