Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template resolver for a new location for uswds templates #400

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,11 @@ The address fragment has two required parameters, `validate` and `inputName`.

- `validate` is a boolean value that determines whether the address should be validated by Smarty
- `inputName` is the name that will be associated with all of the above inputs by being used as a
prefix in their input's name. For example, if the `inputName` is `homeAddress` then the corresponding
inputs will be `homeAddressStreetAddress1`, `homeAddressStreetAddress2`, `homeAddressCity`, `homeAddressState`,
and `homeAddressZipCode`.
prefix in their input's name. For example, if the `inputName` is `homeAddress` then the
corresponding
inputs will
be `homeAddressStreetAddress1`, `homeAddressStreetAddress2`, `homeAddressCity`, `homeAddressState`,
and `homeAddressZipCode`.

The address fragment has five optional
parameters: `streetAddressHelpText`, `streetAddress2HelpText`, `cityHelpText`, `stateHelpText`
Expand All @@ -1195,7 +1197,8 @@ Please note that when using the address fragment you will need to create corresp
your
flow inputs class for each of the above-mentioned inputs created by the fragment. For example, if
your
address fragments input name is `mailingAddress`, then you will need to create the following fields in
address fragments input name is `mailingAddress`, then you will need to create the following fields
in
your flow inputs class:

```
Expand Down Expand Up @@ -2347,6 +2350,17 @@ form-flow:
path: 'name-of-file.yaml'
```

#### Design System

We are moving towards using a [custom theme](https://codeforamerica.github.io/uswds/dist/) of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might reword this. If I'm correct, it's actually setting the UWDS resolver to be used over the default, meaning that enabling it would turn on the USWDS resolver, but those templates will now replace the same ones from the Honeycrisp resolver? Meaning that turning this on will switch your application to using USWDS styles and components instead of Honeycrisp styles and components.

the [US Web Design System(USWDS)](https://designsystem.digital.gov/). Enabling this
property will add another template location in Thymeleaf for USWDS templates to be consumed.

| Property | Default | Description |
|--------------------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------|
| `form-flow.design-system.name` | none | Can use `cfa-uswds` to enable the new CfA USWDS design system assets and templates. Otherwise Honeycrisp assets and templates are used. |
|

#### File upload properties

| Property | Default | Description |
Expand Down
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package formflow.library.config;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.thymeleaf.TemplateEngine;
import org.junit.jupiter.api.Test;

@ImportAutoConfiguration(ThymeleafAutoConfiguration.class)
@SpringBootTest(classes = {ThymeleafConfiguration.class}, properties = {"form-flow.design-system.name=cfa-uswds"})
public class ThymeleafConfigurationLoadedTest {

@Autowired
private TemplateEngine templateEngine;

@Test
void checkThatUSWDSTemplateResolverIsLoaded() {
assertThat(templateEngine.getTemplateResolvers().size()).isEqualTo(2);
var uswdsResolver = templateEngine.getTemplateResolvers().stream()
.filter(x -> Objects.equals(x.getName(), "org.thymeleaf.templateresolver.ClassLoaderTemplateResolver")).findFirst();
assertThat(uswdsResolver).isPresent();
}
}
Copy link
Contributor

@spokenbird spokenbird Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran into this same issue when setting application properties with regards to whether or not to scan file uploads with ClamAV. I could not figure out a way to have different application properties on a test by test basis, even with nested classes.

I hate that it has to be done this way as two seperate tests, and I hate even more that I have to ask:

Do we want a third test that tests it defaults to Honeycrisp when no application property is set?

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package formflow.library.config;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Objects;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.thymeleaf.TemplateEngine;

@ImportAutoConfiguration(ThymeleafAutoConfiguration.class)
@SpringBootTest(classes = {ThymeleafConfiguration.class}, properties = {"form-flow.design-system.name=honeycrisp"})
public class ThymeleafConfigurationUnloadedTest {

@Autowired
private TemplateEngine templateEngine;

@Test
void checkThatUSWDSTemplateResolverIsLoaded() {
assertThat(templateEngine.getTemplateResolvers().size()).isEqualTo(1);
var uswdsResolver = templateEngine.getTemplateResolvers().stream()
.filter(x -> Objects.equals(x.getName(), "org.thymeleaf.templateresolver.ClassLoaderTemplateResolver")).findFirst();
assertThat(uswdsResolver).isNotPresent();
}
}
Loading