zoukankan      html  css  js  c++  java
  • freemarker语法简介

    ftl是一种模板标记语言,用于渲染数据,输入html结构。语法简介如下:

    ${book.name}

    ${book.name?if_exists} //值是否存在

    ${book.name??} //值是否存在

    ${book.name?has_content} //值是否存在

    ${book.name?default("xxx")} //默认值xxx

    ${book.name!"xxx"} //默认值xxx

    ${book.date?string("yyyy-mm-dd")} //日期格式

    <#setting datetime_format="yyyy-MM-dd"/>   ${post.pubTime?number_to_datetime}  //日期格式,long型转换成yyyy-MM-dd格式

    ${book?string.number} //数字格式,20

    ${book?string.currency} //$20.00

    ${book?string.percent} //20%

    <#assign foo=true> //声明变量
    ${foo?string("yes","no")} //yes

    使用lt,gt,lte,gte来代替比较符号

    内置函数:使用?来代替 eg.${book?html}
    字符串:
    html---对字符串进行html编码
    cap_first---字符串首字母大写
    lower_case---将字符串转化成小写
    trim---去掉字符前后的空白字符

    sequences(序列):
    ${list?size}  size---获得序列中元素的个数

    逻辑判断

    <#if condition>....<#elseif condition2>...<#else>....</#if>

    快速定义int区间的集合
    <#assign len = 0..100> //不需要使用[]

    循环读取集合
    <#list items as item> ${item} </#list>
    item_index:当前变量的索引值
    item_has_next:是否存在下一个对象
    <#if items?size != 0></#if> : 判断集合的长度

    宏/模板
    定义宏:<#macro greet><p>段落段落段落<p></#macro>
    使用宏:<@greet></@greet>
    定义宏的参数:跟在宏的名字后面
    <#macro greet person color><p>段落段落段落<p></#macro>
    调用参数时,不用关心顺序问题:<@greet person="John" color="red"></@greet>
    使用<#nested>嵌套宏调用

    hash与list的定义
    <#assign c={"a":"aaa","b":"bbbb"}>
    ${c.a}     //'aaa'

    <#assign c=[1,2,3,4,5]>
    <#list c[1..4] as v>
    ${v}
    </#list>

    用compress来处理输出
    <#compress>....</#compress>消除空白行
    <@compress single_line=true>...</@compress>压缩为一行

    ${}为变量的渲染显示,<>定义操作符, ? 函数名字

    ftl指令规则:
    1.开始标签<#if ...>
    2.结束标签</#if>
    3.空标签 <#assign x=2.582/>
    标签前面的符号是#时,指的是系统内建指令。
    标签前面的符号是@时,指的是用户指令。

     参考:http://www.havenliu.com/goodarticle/420.html

  • 相关阅读:
    模块与包的导入
    递归
    day04
    装饰器2_根据认证来源判断用户和计算登录时间
    装饰器1_统计时间函数装饰欢迎登录函数
    tail -f a.txt | grep 'python'
    函数
    内置函数1
    python模块整理
    VBS恶作剧代码
  • 原文地址:https://www.cnblogs.com/zourong/p/4775991.html
Copyright © 2011-2022 走看看