freemarker 入门语法
https://freemarker.apache.org/docs/index.html
程序员开胃菜
https://freemarker.apache.org/docs/pgui.html
import java.io.File; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import freemarker.template.TemplateExceptionHandler; public class HelloFreemarker { public static void main(String[] args) throws IOException, TemplateException { // 第一步 Create a configuration instance Configuration cfg = new Configuration(Configuration.VERSION_2_3_29); cfg.setDirectoryForTemplateLoading(new File("D://")); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); cfg.setLogTemplateExceptions(false); cfg.setWrapUncheckedExceptions(true); cfg.setFallbackOnNullLoopVariable(false); // 第二步 Create a data-model Map<String, Object> root = new HashMap<>(); root.put("user", "Big Joe"); // 第三步 Get the template Template temp = cfg.getTemplate("test.ftlh"); // 第四步 Merging the template with the data-model Writer out = new OutputStreamWriter(System.out); temp.process(root, out); } }
模板路径D://test.ftlh ,模板文件名test.ftlh
${somedata}
${some.data}
<#list items as item> ${item} </#list>
<#if somedata?exists> </#if>
移除后四位并把首字母大写
${somedata?substring(0,somedata?length-4)?cap_first}
嵌套循环
<#list items as item> <#list item as citem> ${citem} </#list> <#/list>
循环,字符串切割,值等,key存在
<#list data.items as item> ${item.name?split(",")[0]} ${item.name?split(",")[1]} <#if item.type == 'date'>aaaaa</#if> <#if item.child?exists>bbbbb</#if> </#list>
循环map
Map<String, Object> root = new HashMap<>(); HashMap<String, String> map = new HashMap<String,String>(); map.put("name", "小明"); map.put("age","12"); root.put("userMap", map);
<#list userMap?keys as key> ${key}:${userMap[key]} </#list>
赋值
<#assign foo = true> ${foo?then('Y', 'N')}
语法列表
《使用手册》
https://freemarker.apache.org/docs/ref.html
内置函数 ?调用 (举例:${"foo"?cap_first}
- abs
- absolute_template_name
- ancestors
- api
- boolean
- byte
- c for strings, for booleans
- cap_first
- capitalize
- ceiling
- children
- chop_linebreak
- chunk
- contains
- counter
- date for dates, for strings
- date_if_unknown
- datetime for dates, for strings
- datetime_if_unknown
- double
- drop_while
- esc
- ends_with
- ensure_ends_with
- ensure_starts_with
- eval
- eval_json
- filter
- first
- floor
- groups
- float
- has_api
- has_content
- has_next
- html
- index
- index_of
- int
- interpret
- item_cycle
- item_parity
- item_parity_cap
- is_even_item
- is_first
- is_infinite
- is_last
- is_nan
- is_odd_item
- is_type
- iso, iso_...
- j_string
- join
- js_string
- keep_after
- keep_after_last
- keep_before
- keep_before_last
- keys
- last
- last_index_of
- left_pad
- length
- long
- lower_abc
- lower_case
- map
- markup_string
- matches
- max
- min
- namespace
- new
- next_sibling
- no_esc
- node_namespace
- node_name
- node_type
- number
- number_to_date, number_to_datetime, number_to_time
- parent
- previous_sibling
- replace
- remove_beginning
- remove_ending
- reverse
- right_pad
- round
- root
- rtf
- short
- size
- sort
- seq_contains
- seq_index_of
- seq_last_index_of
- sequence
- sort_by
- split
- starts_with
- string: for strings, for numbers, for booleans, for date/time/date-time
- substring (deprecated)
- switch
- take_while
- then
- time for date/time/date-time, for strings
- time_if_unknown
- trim
- truncate, truncate_...
- uncap_first
- upper_abc
- upper_case
- url
- values
- with_args
- with_args_last
- word_list
- xhtml
- xml
模板命令<# ...> 调用(举例:<#list ['a','b'] as name>)
- assign
- attempt
- autoesc
- break: in switch, in list
- case
- compress
- continue
- default
- else: in if, in list
- elseif
- escape
- fallback
- function
- flush
- ftl
- global
- if
- import
- include
- items
- list
- local
- lt
- macro
- nested
- noautoesc
- noescape
- noparse
- nt
- outputformat
- recover
- recurse
- return: in macro, in function
- rt
- sep
- setting
- stop
- switch
- t
- User-defined directive (<@...>)
- visit
归类
- Built-ins for strings(字符串)
- Built-ins for numbers(数字)
- Built-ins for date/time/date-time values(日期)
- Built-ins for booleans(布尔)
- Built-ins for sequences(数组)
- Built-ins for hashes(哈希表)
- Built-ins for nodes (for XML)(xml)
- Loop variable built-ins (列表)
- Type independent built-ins
- Seldom used and expert built-ins