allow redefining template functions. wrap exceptions in FunctionException
This commit is contained in:
parent
6128447121
commit
1e46b1f207
|
@ -3,7 +3,7 @@
|
||||||
functions added by default in all jtwig function repository objects"
|
functions added by default in all jtwig function repository objects"
|
||||||
(:import (com.lyncode.jtwig.functions JtwigFunction)
|
(:import (com.lyncode.jtwig.functions JtwigFunction)
|
||||||
(com.lyncode.jtwig.functions.repository DefaultFunctionRepository)
|
(com.lyncode.jtwig.functions.repository DefaultFunctionRepository)
|
||||||
(com.lyncode.jtwig.functions.exceptions FunctionNotFoundException))
|
(com.lyncode.jtwig.functions.exceptions FunctionNotFoundException FunctionException))
|
||||||
(:require [clj-jtwig.convert :refer [java->clojure clojure->java]]))
|
(:require [clj-jtwig.convert :refer [java->clojure clojure->java]]))
|
||||||
|
|
||||||
(defn- create-function-repository []
|
(defn- create-function-repository []
|
||||||
|
@ -31,13 +31,15 @@
|
||||||
f is returned to the template.
|
f is returned to the template.
|
||||||
prefer to use the 'deftwigfn' macro when possible."
|
prefer to use the 'deftwigfn' macro when possible."
|
||||||
[name f]
|
[name f]
|
||||||
(if (function-exists? name)
|
(let [handler (reify JtwigFunction
|
||||||
(throw (new Exception (str "JTwig template function \"" name "\" already defined.")))
|
(execute [_ arguments]
|
||||||
(let [handler (reify JtwigFunction
|
(try
|
||||||
(execute [_ arguments]
|
(clojure->java (apply f (map java->clojure arguments)))
|
||||||
(clojure->java (apply f (map java->clojure arguments)))))]
|
(catch Exception ex
|
||||||
(.add @functions handler name (make-array String 0))
|
(println "exception!")
|
||||||
(.retrieve @functions name))))
|
(throw (new FunctionException (.getMessage ex) ex))))))]
|
||||||
|
(.add @functions handler name (make-array String 0))
|
||||||
|
(.retrieve @functions name)))
|
||||||
|
|
||||||
(defmacro deftwigfn
|
(defmacro deftwigfn
|
||||||
"adds a new template function. templates can call it by by the name specified and passing in the
|
"adds a new template function. templates can call it by by the name specified and passing in the
|
||||||
|
|
Reference in a new issue