diff --git a/README.md b/README.md index 31fd0a9..b7c5744 100644 --- a/README.md +++ b/README.md @@ -37,17 +37,20 @@ Add the dependency (for all available versions see [https://jitpack.io/#Serbroda ``` -Create a bean of `ComponentDialect`. You can also register parsers to add components automatically. The `StandardThymeleafComponentParser` searches for all `th:fragment` attributes and registers them as components. +**With Spring Boot 3.x:** No configuration needed. The dialect is auto-configured and scans `templates/components/` for component fragments. + +**Custom configuration:** To customize the component directory or register components manually, define your own `ComponentDialect` bean (the auto-configuration will back off): ```java @Bean public ComponentDialect componentDialect() { ComponentDialect dialect = new ComponentDialect(); - dialect.addParser(new StandardThymeleafComponentParser("templates/", ".html", "components")); + dialect.addParser(new StandardThymeleafComponentParser("templates/", ".html", "my-components")); return dialect; } ``` +**Without Spring Boot:** Register the dialect manually with your `TemplateEngine`. Usage ----- diff --git a/pom.xml b/pom.xml index aacf6de..f6753e2 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,12 @@ thymeleaf 3.1.3.RELEASE + + org.springframework.boot + spring-boot-autoconfigure + 3.4.3 + true + org.junit.jupiter junit-jupiter diff --git a/src/main/java/de/morphbit/thymeleaf/autoconfigure/ComponentDialectAutoConfiguration.java b/src/main/java/de/morphbit/thymeleaf/autoconfigure/ComponentDialectAutoConfiguration.java new file mode 100644 index 0000000..663fe5c --- /dev/null +++ b/src/main/java/de/morphbit/thymeleaf/autoconfigure/ComponentDialectAutoConfiguration.java @@ -0,0 +1,41 @@ +package de.morphbit.thymeleaf.autoconfigure; + +import de.morphbit.thymeleaf.dialect.ComponentDialect; +import de.morphbit.thymeleaf.parser.StandardThymeleafComponentParser; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; + +/** + * Spring Boot auto-configuration for the Thymeleaf Component Dialect. + * + *

+ * Automatically registers a {@link ComponentDialect} bean with a + * {@link StandardThymeleafComponentParser} that scans for component fragments + * based on {@link ComponentDialectProperties}. + *

+ * + *

+ * This auto-configuration is only active when Thymeleaf is on the classpath. To + * fully customize the dialect, define your own {@link ComponentDialect} bean + * and this auto-configuration will back off. + *

+ * + * @see ComponentDialectProperties + */ +@AutoConfiguration +@ConditionalOnClass(name = "org.thymeleaf.spring6.SpringTemplateEngine") +@EnableConfigurationProperties(ComponentDialectProperties.class) +public class ComponentDialectAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public ComponentDialect componentDialect(ComponentDialectProperties properties) { + var dialect = new ComponentDialect(); + dialect.addParser(new StandardThymeleafComponentParser(properties.getTemplatePrefix(), + properties.getTemplateSuffix(), properties.getComponentDirectory())); + return dialect; + } +} diff --git a/src/main/java/de/morphbit/thymeleaf/autoconfigure/ComponentDialectProperties.java b/src/main/java/de/morphbit/thymeleaf/autoconfigure/ComponentDialectProperties.java new file mode 100644 index 0000000..39799b6 --- /dev/null +++ b/src/main/java/de/morphbit/thymeleaf/autoconfigure/ComponentDialectProperties.java @@ -0,0 +1,49 @@ +package de.morphbit.thymeleaf.autoconfigure; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for the Thymeleaf Component Dialect. + * + *

+ * Example {@code application.yml}: + *

+ * + *
{@code
+ * thymeleaf-component-dialect:
+ *   template-prefix: templates/
+ *   template-suffix: .html
+ *   component-directory: components
+ * }
+ */ +@ConfigurationProperties(prefix = "thymeleaf-component-dialect") +public class ComponentDialectProperties { + + private String templatePrefix = "templates/"; + private String templateSuffix = ".html"; + private String componentDirectory = "components"; + + public String getTemplatePrefix() { + return templatePrefix; + } + + public void setTemplatePrefix(String templatePrefix) { + this.templatePrefix = templatePrefix; + } + + public String getTemplateSuffix() { + return templateSuffix; + } + + public void setTemplateSuffix(String templateSuffix) { + this.templateSuffix = templateSuffix; + } + + public String getComponentDirectory() { + return componentDirectory; + } + + public void setComponentDirectory(String componentDirectory) { + this.componentDirectory = componentDirectory; + } +} diff --git a/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..9233e5f --- /dev/null +++ b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +de.morphbit.thymeleaf.autoconfigure.ComponentDialectAutoConfiguration