SpringBoot整合Freemarker(三)

定义循环输出的宏

java 复制代码
<#macro list title items>
${title?cap_first}:
        <#list items as x>
           *${x?cap_first}
        </#list>
</#macro>

<@list items=["mouse", "elephant", "python"]  title="Animals"/>

输出结果:

Animals:

*Mouse

*Elephant

*Python

包含body 的宏

java 复制代码
<#macro repeat count>
  <#list 1..count as x>
         <#nested x, x/2, x==count>
  </#list>
</#macro>

<@repeat count=4 ; c,halfc,last>
  ${c}. ${halfc}<#if last> Last!</#if>
</@repeat> 

输出:

  1. 0.5

  2. 1

  3. 1.5

  4. 2 Last!

include关键字

语法:

java 复制代码
<#include filename options>
options包含两个属性:
encoding="GBK" 编码格式;
parse=true 是否作为ftl语法解析,默认是true,false就是以文本方式引入

例子:

sql 复制代码
/common/copyright.ftl 包含内容:
Copyright 2001-2002 ${me}
All rights reserved.  
----------------------------------------------------
<#assign me = "Juila Smith">
Some test
Yeah
<#include "/common/copyright.ftl" encoding="GBK">

Import关键字

语法:

sql 复制代码
<#import path as hash>
类似于java里的import,它导入文件,然后就可以在当前文件里使用被导入文件里的宏组件
用例
假设mylib.ftl 里定义了宏copyright 那么我们在其他模板页面里可以这样使用
<#import "/libs/mylib.ftl" as my>
<@my.copyright  date="1999-2002"/>
<#-- "my"在freemarker里被称作namespace -->	

compress关键字

语法:

java 复制代码
<#compress>
  ...
</#compress>

用来压缩空白空间和空白的行

相关推荐
得物技术14 小时前
# 一、项目概览 Dragonboat 是纯 Go 实现的(multi-group)Raft 库。 为应用屏蔽 Raft 复杂性,提供易于使用的 NodeH
后端
4***149014 小时前
Rust系统工具开发实践指南
开发语言·后端·rust
s***353015 小时前
Spring Boot3.x集成Flowable7.x(一)Spring Boot集成与设计、部署、发起、完成简单流程
java·spring boot·后端
大头an15 小时前
Spring Boot CLI 从入门到企业级实战(上下篇)
后端
3***161015 小时前
【监控】Spring Boot+Prometheus+Grafana实现可视化监控
spring boot·grafana·prometheus
s***45315 小时前
SpringBoot返回文件让前端下载的几种方式
前端·spring boot·后端
元Y亨H15 小时前
一文读懂计算机系统核心流量术语
后端
q***064715 小时前
SpringSecurity相关jar包的介绍
android·前端·后端
陈橘又青15 小时前
100% AI 写的开源项目三周多已获得 800 star 了
人工智能·后端·ai·restful·数据
2501_9411486116 小时前
C++实时数据处理实战:多线程与异步IO结合高性能代码解析
java·后端·struts