| 程序包 | 说明 |
|---|---|
| com.jfinal.kit | |
| com.jfinal.plugin.activerecord.sql | |
| com.jfinal.template | |
| com.jfinal.template.ext.directive | |
| com.jfinal.template.stat | |
| com.jfinal.template.stat.ast |
| 限定符和类型 | 类和说明 |
|---|---|
static class |
ElKit.InnerEvalDirective |
| 限定符和类型 | 类和说明 |
|---|---|
class |
NameSpaceDirective
NameSpaceDirective
|
class |
ParaDirective
#para 指令用于在 sql 模板中根据参数名生成问号占位以及查询参数
一、参数为表达式的用法
1:模板内容
#sql("find")
select * from user where nickName = #para(nickName) and age > #para(age)
#end
2: java 代码
SqlPara sp = getSqlPara("find", Kv.by("nickName", "prettyGirl").set("age", 18));
user.find(sp)
或者:
user.find(sp.getSql(), sp.getPara());
3:以上用法会在 #para(expr) 处生成问号占位字符,并且实际的参数放入 SqlPara 对象的参数列表中
后续可以通过 sqlPara.getPara() 获取到参数并直接用于查询
二、参数为 int 型数字的用法
1:模板内容
#sql("find")
select * from user where id > #para(0) and id < #para(1)
#end
2: java 代码
SqlPara sp = getSqlPara("find", 10, 100);
user.find(sp)
3:以上用法会在 #para(0) 与 #para(1) 处生成问号占位字符,并且将 10、100 这两个参数放入
SqlPara 对象的参数列表中,后续可以通过 sqlPara.getPara() 获取到参数并直接用于查询
|
class |
SqlDirective
SqlDirective
|
| 限定符和类型 | 类和说明 |
|---|---|
class |
Directive
Directive 供用户继承并扩展自定义指令,具体用法可以参考
com.jfinal.template.ext.directive 包下面的例子
|
| 限定符和类型 | 字段和说明 |
|---|---|
protected Stat |
Directive.stat
具有 #end 结束符的指令内部嵌套的所有内容,调用 stat.exec(env, scope, writer)
即可执行指令内部嵌入所有指令与表达式,如果指令没有 #end 结束符,该属性无效
|
| 限定符和类型 | 方法和说明 |
|---|---|
Stat |
EngineConfig.getDirective(String directiveName) |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
Directive.setStat(Stat stat)
指令被解析时注入指令 body 内容,仅对于具有 #end 结束符的指令有效
|
| 构造器和说明 |
|---|
Template(Env env,
Stat ast) |
| 限定符和类型 | 类和说明 |
|---|---|
class |
DateDirective
不带参时,按默认 pattern 输出当前日期
#date() 指令支持无参时获取当前指令,第一个参数 string 当成是 pattern
日期输出指令,第一个参数是被输出的 java.util.Date 对象或其子类对象
无第二个参数时按默认 patter 输出,第二个参数为 expr 表达式,表示 pattern
第二个为 date 时,表示当第一个为 null 时的默认值
|
class |
EscapeDirective
Escape 对字符串进行转义
用法:
#escape(value)
|
class |
NowDirective
输出当前时间,默认考虑是输出时间,给 pattern 输出可能是 Date、DateTime、Timestamp
带 String 参数,表示 pattern
|
class |
RandomDirective
输出随机数
|
class |
RenderDirective
#render 指令用于动态渲染子模板,作为 include 指令的补充
两种用法:
1:只传入一个参数,参数可以是 String 常量,也可以是任意表达式
#render("_hot.html")
#render(subFile)
2:传入任意多个参数,除第一个参数以外的所有参数必须是赋值表达式,用于实现参数传递功能
#render("_hot.html", title = "热门新闻", list = newsList)
上例中传递了 title、list 两个参数,可以代替父模板中的 #set 指令传参方式
并且此方式传入的参数只在子模板作用域有效,不会污染父模板作用域
这种传参方式有利于将子模板模块化,例如上例的调用改成如下的参数:
#render("_hot.html", title = "热门项目", list = projectList)
通过这种传参方式在子模板 _hot.html 之中,完全不需要修改对于 title 与 list
这两个变量的处理代码,就实现了对 “热门项目” 数据的渲染
|
class |
StringDirective
#string 指令方便定义大量的多行文本变量,这个是 java 语言中极为需要的功能
定义:
#string(name)
在此是大量的字符串
#end
使用:
#(name)
|
| 限定符和类型 | 方法和说明 |
|---|---|
Stat |
Parser.parse() |
| 限定符和类型 | 类和说明 |
|---|---|
class |
Break
Break
java 中 break、continue 可出现在 for 中的最后一行,不一定要套在 if 中
|
class |
Call
Call 调用模板函数,两种用法:
1:常规调用
#@funcName(p1, p2, ..., pn)
2:安全调用,函数被定义才调用,否则跳过
#@funcName?
|
class |
Continue
Continue
|
class |
Define
Define 定义模板函数:
#define funcName(p1, p2, ..., pn)
body
#end
模板函数类型:
1:全局共享的模板函数
通过 engine.addSharedFunction(...)
|
class |
Else
Else
|
class |
ElseIf
ElseIf
|
class |
For
For 循环控制,支持 List、Map、数组、Collection、Iterator、Iterable
Enumeration、null 以及任意单个对象的迭代,简单说是支持所有对象迭代
主要用法:
1:#for(item : list) #(item) #end
2:#for(item : list) #(item) #else content #end
3:#for(i=0; i<9; i++) #(item) #end
4:#for(i=0; i<9; i++) #(item) #else content #end
|
class |
If
If
|
class |
Include
Include
1:父模板被缓存时,被 include 的模板会被间接缓存,无需关心缓存问题
2:同一个模板文件被多个父模板 include,所处的背景环境不同,例如各父模板中定义的模板函数不同
各父模板所处的相对路径不同,所以多个父模板不能共用一次 parse 出来的结果,而是在每个被include
的地方重新 parse
两种用法:
1:只传入一个参数,参数必须是 String 常量,如果希望第一个参数是变量可以使用 #render 指令去实现
#include("_hot.html")
2:传入任意多个参数,除第一个参数以外的所有参数必须是赋值表达式,用于实现参数传递功能
#include("_hot.html", title = "热门新闻", list = newsList)
上例中传递了 title、list 两个参数,可以代替父模板中的 #set 指令传参方式
并且此方式传入的参数只在子模板作用域有效,不会污染父模板作用域
这种传参方式有利于将子模板模块化,例如上例的调用改成如下的参数:
#include("_hot.html", title = "热门项目", list = projectList)
通过这种传参方式在子模板 _hot.html 之中,完全不需要修改对于 title 与 list
这两个变量的处理代码,就实现了对 “热门项目” 数据的渲染
|
class |
Output
Output 输出指令
用法:
1:#(value)
2:#(x = 1, y = 2, x + y)
3:#(seoTitle ??
|
class |
Return
Return
通常用于 #define 指令内部,不支持返回值
|
class |
Set
Set 赋值,从内向外作用域查找变量,找到则替换变量值,否则在顶层作用域赋值
用法:
1:#set(k = v)
2:#set(k1 = v1, k2 = v2, ..., kn = vn)
3:#set(x = 1+2)
4:#set(x = 1+2, y = 3>4, ..., z = c ?
|
class |
SetGlobal
SetLocal 设置全局变量,全局作用域是指本次请求的整个 template
适用于极少数的在内层作用域中希望直接操作顶层作用域的场景
|
class |
SetLocal
SetLocal 设置局部变量
通常用于 #define #include 指令内部需要与外层作用域区分,以便于定义重用型模块的场景
也常用于 #for 循环内部的临时变量
|
class |
StatList
StatList
|
class |
Text
Text 输出纯文本块以及使用 "#[[" 与 "]]#" 指定的非解析块
|
| 限定符和类型 | 字段和说明 |
|---|---|
static Stat[] |
StatList.NULL_STATS |
| 限定符和类型 | 方法和说明 |
|---|---|
Stat |
StatList.getStat(int index) |
Stat |
Stat.setLocation(Location location) |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
Stat.setStat(Stat stat) |
void |
If.setStat(Stat elseIfOrElse)
take over setStat(...) method of super class
|
void |
ElseIf.setStat(Stat elseIfOrElse)
take over setStat(...) method of super class
|
| 构造器和说明 |
|---|
Define(String functionName,
ExprList exprList,
Stat stat,
Location location) |
Else(Stat stat) |
ElseIf(ExprList cond,
Stat stat,
Location location) |
For(ForCtrl forCtrl,
StatList statList,
Stat _else) |
If(ExprList cond,
Stat stat,
Location location) |
| 构造器和说明 |
|---|
StatList(List<Stat> statList) |
Copyright © 2017. All rights reserved.