Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ Add the dependency (for all available versions see [https://jitpack.io/#Serbroda
</dependency>
```

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
-----
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<artifactId>thymeleaf</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.4.3</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* Automatically registers a {@link ComponentDialect} bean with a
* {@link StandardThymeleafComponentParser} that scans for component fragments
* based on {@link ComponentDialectProperties}.
* </p>
*
* <p>
* 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.
* </p>
*
* @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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.morphbit.thymeleaf.autoconfigure;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Configuration properties for the Thymeleaf Component Dialect.
*
* <p>
* Example {@code application.yml}:
* </p>
*
* <pre>{@code
* thymeleaf-component-dialect:
* template-prefix: templates/
* template-suffix: .html
* component-directory: components
* }</pre>
*/
@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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
de.morphbit.thymeleaf.autoconfigure.ComponentDialectAutoConfiguration
Loading