浏览代码

jfinal 3.6

James 7 年之前
父节点
当前提交
f36dfb9399

+ 1 - 1
src/main/java/com/jfinal/template/EngineConfig.java

@@ -73,9 +73,9 @@ public class EngineConfig {
 		addDirective("string", StringDirective.class);
 		addDirective("string", StringDirective.class);
 		addDirective("random", RandomDirective.class);
 		addDirective("random", RandomDirective.class);
 		addDirective("number", NumberDirective.class);
 		addDirective("number", NumberDirective.class);
+		addDirective("call", CallDirective.class);
 		
 		
 		// Add official shared method of Template Engine
 		// Add official shared method of Template Engine
-		// addSharedMethod(new Json());
 		addSharedMethod(new SharedMethodLib());
 		addSharedMethod(new SharedMethodLib());
 	}
 	}
 	
 	

+ 40 - 7
src/main/java/com/jfinal/template/ext/directive/CallDirective.java

@@ -19,6 +19,8 @@ package com.jfinal.template.ext.directive;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import com.jfinal.template.Directive;
 import com.jfinal.template.Directive;
 import com.jfinal.template.Env;
 import com.jfinal.template.Env;
+import com.jfinal.template.TemplateException;
+import com.jfinal.template.expr.ast.Const;
 import com.jfinal.template.expr.ast.Expr;
 import com.jfinal.template.expr.ast.Expr;
 import com.jfinal.template.expr.ast.ExprList;
 import com.jfinal.template.expr.ast.ExprList;
 import com.jfinal.template.io.Writer;
 import com.jfinal.template.io.Writer;
@@ -35,8 +37,11 @@ import com.jfinal.template.stat.ast.Define;
  *     #call(funcName, p1, p2, ..., pn)
  *     #call(funcName, p1, p2, ..., pn)
  *     其中 funcName,为函数名,p1、p2、pn 为被调用函数所使用的参数
  *     其中 funcName,为函数名,p1、p2、pn 为被调用函数所使用的参数
  * 
  * 
- * 注意:该指令并非默认指令,需要使用 Engine.addDirective("call", CallDirective.class)
- *      配置后才可以使用
+ * 
+ * 如果希望模板函数不存在时忽略其调用,添加常量值 true 在第一个参数位置即可
+ * 例如:
+ *     #call(true, funcName, p1, p2, ..., pn)
+ * 
  * 
  * 
  * TODO 后续优化看一下 ast.Call.java
  * TODO 后续优化看一下 ast.Call.java
  */
  */
@@ -45,27 +50,55 @@ public class CallDirective extends Directive {
 	protected Expr funcNameExpr;
 	protected Expr funcNameExpr;
 	protected ExprList paraExpr;
 	protected ExprList paraExpr;
 	
 	
+	protected boolean nullSafe = false;		// 是否支持函数名不存在时跳过
+	
 	public void setExprList(ExprList exprList) {
 	public void setExprList(ExprList exprList) {
-		if (exprList.length() == 0) {
+		int len = exprList.length();
+		if (len == 0) {
 			throw new ParseException("模板函数名不能缺失", location);
 			throw new ParseException("模板函数名不能缺失", location);
 		}
 		}
 		
 		
-		this.funcNameExpr = exprList.getExpr(0);
+		int index = 0;
+		Expr expr = exprList.getExpr(index);
+		if (expr instanceof Const && ((Const)expr).isBoolean()) {
+			if (len == 1) {
+				throw new ParseException("模板函数名不能缺失", location);
+			}
+			
+			nullSafe = ((Const)expr).getBoolean();
+			index++;
+		}
 		
 		
+		funcNameExpr = exprList.getExpr(index++);
 		ArrayList<Expr> list = new ArrayList<Expr>();
 		ArrayList<Expr> list = new ArrayList<Expr>();
-		for (int i=1; i<exprList.length(); i++) {
+		for (int i=index; i<len; i++) {
 			list.add(exprList.getExpr(i));
 			list.add(exprList.getExpr(i));
 		}
 		}
-		this.paraExpr = new ExprList(list);
+		paraExpr = new ExprList(list);
 	}
 	}
 	
 	
 	public void exec(Env env, Scope scope, Writer writer) {
 	public void exec(Env env, Scope scope, Writer writer) {
 		Object funcNameValue = funcNameExpr.eval(scope);
 		Object funcNameValue = funcNameExpr.eval(scope);
+		if (funcNameValue == null) {
+			if (nullSafe) {
+				return ;
+			}
+			throw new TemplateException("模板函数不存在 : " + funcNameValue, location);
+		}
+		
 		if (!(funcNameValue instanceof String)) {
 		if (!(funcNameValue instanceof String)) {
-			throw new ParseException("模板函数名必须是字符串", location);
+			throw new TemplateException("模板函数名必须是字符串", location);
 		}
 		}
 		
 		
 		Define func = env.getFunction(funcNameValue.toString());
 		Define func = env.getFunction(funcNameValue.toString());
+		
+		if (func == null) {
+			if (nullSafe) {
+				return ;
+			}
+			throw new TemplateException("模板函数不存在 : " + funcNameValue, location);
+		}
+		
 		func.call(env, scope, paraExpr, writer);
 		func.call(env, scope, paraExpr, writer);
 	}
 	}
 }
 }