浏览代码

添加 #returnIf(expr) 指令

James 2 年之前
父节点
当前提交
1ffd29d4e4
共有 1 个文件被更改,包括 35 次插入0 次删除
  1. 35 0
      src/main/java/com/jfinal/template/stat/ast/ReturnIf.java

+ 35 - 0
src/main/java/com/jfinal/template/stat/ast/ReturnIf.java

@@ -0,0 +1,35 @@
+package com.jfinal.template.stat.ast;
+
+import com.jfinal.template.Env;
+import com.jfinal.template.expr.ast.Expr;
+import com.jfinal.template.expr.ast.ExprList;
+import com.jfinal.template.expr.ast.Logic;
+import com.jfinal.template.io.Writer;
+import com.jfinal.template.stat.Location;
+import com.jfinal.template.stat.ParseException;
+import com.jfinal.template.stat.Scope;
+
+/**
+ * #returnIf(expr) 指令,当 expr 为 true 时返回,等价于:
+ *     #if (expr)
+ *         #return
+ *     #end
+ */
+public class ReturnIf extends Stat {
+
+    final Expr expr;
+
+    public ReturnIf(ExprList exprList, Location location) {
+        if (exprList.length() == 0) {
+            throw new ParseException("The parameter of #returnIf directive can not be blank", location);
+        }
+        this.expr = exprList.getActualExpr();
+    }
+
+    @Override
+    public void exec(Env env, Scope scope, Writer writer) {
+        if (Logic.isTrue(expr.eval(scope))) {
+            scope.getCtrl().setReturn();
+        }
+    }
+}