Skip to content

Commit

Permalink
feat: support freemarker template Inheritance halo-dev#534 (halo-dev#537
Browse files Browse the repository at this point in the history
)

* feat: halo-dev#534

* fix: HashMap initialCapacity
  • Loading branch information
ruibaby authored Feb 1, 2020
1 parent 2855304 commit 63122ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ ext {
levelDbVersion = '0.12'
jsonVersion = '20190722'
fastJsonVersion = '1.2.56'
templateInheritance = "0.4.RELEASE"
}

dependencies {
Expand All @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
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;
Expand All @@ -29,7 +33,9 @@
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;
Expand Down Expand Up @@ -114,6 +120,16 @@ public void addFormatters(FormatterRegistry registry) {
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.
*
Expand Down Expand Up @@ -142,6 +158,13 @@ public FreeMarkerConfigurer freemarkerConfig(HaloProperties haloProperties) thro
// 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;
}

Expand Down

0 comments on commit 63122ef

Please sign in to comment.