feat: support freemarker template Inheritance #534 (#537)

* feat: #534

* fix: HashMap initialCapacity
pull/538/head
Ryan Wang 2020-02-01 17:14:36 +08:00 committed by GitHub
parent 28553042cf
commit 63122ef625
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -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"

View File

@ -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<String, TemplateModel> freemarkerLayoutDirectives() {
Map<String, TemplateModel> 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<String, Object> freemarkerVariables = new HashMap<>(5);
freemarkerVariables.put("layout", freemarkerLayoutDirectives());
configurer.setFreemarkerVariables(freemarkerVariables);
return configurer;
}