woshikid / blog

Apache License 2.0
8 stars 1 forks source link

FreeMarker学习笔记 #162

Open woshikid opened 3 years ago

woshikid commented 3 years ago

POM

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

自动配置类为FreeMarkerAutoConfiguration,默认配置类为FreeMarkerProperties

自定义配置

spring:
  freemarker:
    request-context-attribute: request # ${request.contextPath}
    settings:
      datetime_format: "yyyy-MM-dd HH:mm:ss"
      date_format: "yyyy-MM-dd"
      number_format: ",##0.##"

src/main/resources/templates/test.ftlh

<!DOCTYPE html>
<html>
<body>
    <#global name="value">
    <#assign num=10 user="admin" date=.now timestamp=1612345678000>
    <div>${num*10/10+10-10}</div>
    <div>${num?string.currency}</div>
    <div>${num?string.percent}</div>
    <div>${"hello ${user}"}</div>
    <div>${"hello " + user}</div>
    <div>hello ${user}</div>
    <div>${null!}</div>
    <div>${null!"default"}</div>
    <div>${"hello"?upper_case}</div>
    <div>${"hello"?lower_case}</div>
    <div>${"hello"?cap_first}</div>
    <div>${"hello"?uncap_first}</div>
    <div>${"hello"?trim}</div>
    <div>${"hello"?length}</div>
    <div>${"hello"[0..4]}</div>
    <div>${"hello"?contains("hell")?string("true", "false")}</div>
    <div>${1234.56?int}</div>
    <div>${1234.5678?string("0.##")}</div>
    <div>${list?size}</div>
    <div>${date?string("yyyy-MM-dd")}</div>
    <div>${date?date}</div>
    <div>${date?datetime}</div>
    <div>${timestamp?number_to_datetime}</div>
</body>
</html>

list

<#list [1, 2, 3] as i>${i}</#list>
<#list list as i>${i}</#list>
<#list list + [1, 2, 3] as i>${i_index}${i}</#list>
<#list {"key": "value"}?keys as key>${key}</#list>
<#list map?keys as key>${key}${map[key]}</#list>
<#list (map + {"key": "value"})?values as value>${value}</#list>
<#list 1..10 as i>
    ${i}<#sep>,</#sep>
    <#if !i_has_next></#if>
    <#if i = 5><#break></#if>
</#list>

if

<#if num = 10></#if>
<#if num == 10></#if>
<#if num != 10></#if>
<#if num gt 10></#if>
<#if num gte 10></#if>
<#if num lt 10></#if>
<#if num lte 10></#if>
<#if num??>not null</#if>
<#if (num > 10)>
<#elseif (num < 10)>
<#else>
</#if>

switch

<#switch num>
    <#case 10>10<#break>
    <#case 20>20<#break>
    <#default>0
</#switch>

attempt (try catch)

<#attempt>
    hello ${user}
    <#recover>
    user is null
</#attempt>

src/main/resources/templates/fragments.ftlh

<#macro footer start, end>
    ${start} - ${end} All Rights Reserved
    <#nested>
</#macro>

include

<#include "/fragments.ftlh">
<@footer "2020", "2021">
    nested content
</@footer>

import

<#import "/fragments.ftlh" as footer>
<@footer.footer start="2020" end="2021">
    nested content
</@footer.footer>

国际化

<!-- spring-webmvc.jar!/org/springframework/web/servlet/view/freemarker/spring.ftl -->
<#import "/spring.ftl" as spring>
<@spring.message "welcome.text"/>
<@spring.messageText "welcome.text", "default text"/>
<@spring.messageArgs "welcome.text", [user]/>

手动渲染

@Autowired
Configuration configuration;
// 不支持spring.ftl
Template template = configuration.getTemplate("test.ftlh");
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);