enjoy模板的输出指令#()的参数名有要求吗?目前包含.会报错

enjoy模板的输出指令#()的参数名有要求吗?目前包含.会报错:

 com.jfinal.template.stat.ParseException: Expression error: can not match "."

模板里面使用:

#(ZONGJC.0001-DAYFINISH.FINISH)

我渲染的数据是:

Map<String, Object> dataMap = Kv.by("ZONGJC.0001-DAYFINISH.FINISH", 2000).set("ZONGJC.0001-DAYFINISH.PLAN", 1000);


key是包含.和-的(历史项目,如果要修改key格式很麻烦),有没有办法来支持.??


评论区

JFinal

2017-12-20 15:55

这种格式下,输出指令会将其当成是属性取值,可以很简单扩展一个输出指令来解决:

import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
public class ValueDirective extends Directive {
public void exec(Env env, Scope scope, Writer writer) {
String key = (String)exprList.eval(scope);
if (key != null) {
Object value = scope.get(key);
if (value != null) {
write(writer, value.toString());
}
}
}
}

然后配置一下:
engine.addDirective("value", new ValueDirective());

用的时候注意要这样用:
#value("ZONGJC.0001-DAYFINISH.FINISH");

芦苇

2017-12-20 17:18

OK,谢谢提示啊,赞