diff --git a/build.gradle b/build.gradle index 7c545968e..6ac183adb 100644 --- a/build.gradle +++ b/build.gradle @@ -62,6 +62,7 @@ ext { levelDbVersion = '0.12' jsonVersion = '20190722' fastJsonVersion = '1.2.56' + templateInheritance = "0.4.RELEASE" } dependencies { @@ -70,6 +71,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-undertow' implementation 'org.springframework.boot:spring-boot-starter-freemarker' + implementation "kr.pe.kwonnam.freemarker:freemarker-template-inheritance:$templateInheritance" implementation "io.github.biezhi:oh-my-email:$ohMyEmailVersion" implementation "cn.hutool:hutool-core:$hutoolVersion" diff --git a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java index af06a9b1a..c12c74da7 100644 --- a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java +++ b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java @@ -4,6 +4,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import freemarker.core.TemplateClassResolver; import freemarker.template.TemplateException; import freemarker.template.TemplateExceptionHandler; +import freemarker.template.TemplateModel; +import kr.pe.kwonnam.freemarker.inheritance.BlockDirective; +import kr.pe.kwonnam.freemarker.inheritance.ExtendsDirective; +import kr.pe.kwonnam.freemarker.inheritance.PutDirective; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.jackson.JsonComponentModule; import org.springframework.context.annotation.Bean; @@ -29,7 +33,9 @@ import run.halo.app.model.support.HaloConst; import run.halo.app.security.resolver.AuthenticationArgumentResolver; import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import static run.halo.app.model.support.HaloConst.FILE_SEPARATOR; @@ -114,6 +120,16 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { registry.addConverterFactory(new StringToEnumConverterFactory()); } + @Bean + public Map freemarkerLayoutDirectives() { + Map freemarkerLayoutDirectives = new HashMap<>(5); + freemarkerLayoutDirectives.put("extends", new ExtendsDirective()); + freemarkerLayoutDirectives.put("block", new BlockDirective()); + freemarkerLayoutDirectives.put("put", new PutDirective()); + + return freemarkerLayoutDirectives; + } + /** * Configuring freemarker template file path. * @@ -142,6 +158,13 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { // Set predefined freemarker configuration configurer.setConfiguration(configuration); + // Set layout variable + Map freemarkerVariables = new HashMap<>(5); + + freemarkerVariables.put("layout", freemarkerLayoutDirectives()); + + configurer.setFreemarkerVariables(freemarkerVariables); + return configurer; }