Skip to content

Commit

Permalink
Add template resolver for a new location for uswds templates
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Gonzalez <agonzalez@codeforamerica.org>
Co-authored-by: Devon Hirth <dhirth@codeforamerica.org>
  • Loading branch information
3 people authored and cy-by committed Oct 10, 2023
1 parent b60a7cc commit 01983cd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/main/java/formflow/library/config/ThymeleafConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package formflow.library.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

/**
* Provides configuration for the Thymeleaf templates.
* Provides configuration for the Thymeleaf templates, besides the default.
*/
@Configuration
@Slf4j
public class ThymeleafConfiguration {

/**
* Creates a new SpringResourceTemplateResolver and configures it so it knows where to locate the templates and how to read
* them.
* Creates a new ClassLoaderTemplateResolver to be able to resolve templates in the {@code cfa-uswds-templates/} directory.
*
* <p>
* The resolver will be configured to look in the {@code templates/} directory for {@code .html} files. The assumed encoding
* will be {@code UTF-8}. The templates will not be cached.
* The resolver will be configured to look in the {@code cfa-uswds-templates} directory for {@code .html} files. The assumed
* encoding will be {@code UTF-8}. The templates will not be cached.
* </p>
*
* @return a new resolver configured to work with templates
* @return a new resolver configured to work with library uswds templates.
*/
@Bean
public SpringResourceTemplateResolver secondaryTemplateResolver() {
SpringResourceTemplateResolver secondaryTemplateResolver = new SpringResourceTemplateResolver();
secondaryTemplateResolver.setPrefix("templates/");
@ConditionalOnProperty(name = "form-flow.design-system.name", havingValue = "cfa-uswds")
public ClassLoaderTemplateResolver cfaUswdsTemplateResolver() {
log.info("Template resolution has been set to include the path `cfa-uswds-templates/`.");
ClassLoaderTemplateResolver secondaryTemplateResolver = new ClassLoaderTemplateResolver();
secondaryTemplateResolver.setPrefix("cfa-uswds-templates/");
secondaryTemplateResolver.setSuffix(".html");
secondaryTemplateResolver.setTemplateMode(TemplateMode.HTML);
secondaryTemplateResolver.setCharacterEncoding("UTF-8");
secondaryTemplateResolver.setOrder(1);
secondaryTemplateResolver.setOrder(0);
secondaryTemplateResolver.setCheckExistence(true);
secondaryTemplateResolver.setCacheable(false);

Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<th:block th:fragment="headLinks">
<!-- CFA USWDS Links -->
</th:block>

0 comments on commit 01983cd

Please sign in to comment.