Merge branch 'master' into java6
Conflicts: project.clj
This commit is contained in:
commit
9400b70bdc
|
@ -1,4 +1,4 @@
|
||||||
(defproject clj-jtwig-java6 "0.2"
|
(defproject clj-jtwig-java6 "0.2.1"
|
||||||
:description "Clojure wrapper for JTwig (Java 6 dependencies)"
|
:description "Clojure wrapper for JTwig (Java 6 dependencies)"
|
||||||
:url "https://github.com/gered/clj-jtwig/tree/java6"
|
:url "https://github.com/gered/clj-jtwig/tree/java6"
|
||||||
:license {:name "Apache License, Version 2.0"
|
:license {:name "Apache License, Version 2.0"
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
"wrapper functions for working with JTwig from clojure"
|
"wrapper functions for working with JTwig from clojure"
|
||||||
(:import (com.lyncode.jtwig JtwigTemplate JtwigContext JtwigModelMap)
|
(:import (com.lyncode.jtwig JtwigTemplate JtwigContext JtwigModelMap)
|
||||||
(com.lyncode.jtwig.tree.api Content)
|
(com.lyncode.jtwig.tree.api Content)
|
||||||
(java.io File FileNotFoundException ByteArrayOutputStream))
|
(java.io File FileNotFoundException ByteArrayOutputStream)
|
||||||
|
(java.net URL))
|
||||||
(:require [clojure.walk :refer [stringify-keys]])
|
(:require [clojure.walk :refer [stringify-keys]])
|
||||||
(:use [clj-jtwig.functions]))
|
(:use [clj-jtwig.functions]))
|
||||||
|
|
||||||
|
@ -128,10 +129,11 @@
|
||||||
[]
|
[]
|
||||||
(reset! compiled-templates {}))
|
(reset! compiled-templates {}))
|
||||||
|
|
||||||
(defn- get-resource-path [filename]
|
(defn- get-resource-path
|
||||||
(-> (Thread/currentThread)
|
(^URL [^String filename]
|
||||||
(.getContextClassLoader)
|
(-> (Thread/currentThread)
|
||||||
(.getResource filename)))
|
(.getContextClassLoader)
|
||||||
|
(.getResource filename))))
|
||||||
|
|
||||||
(defn- make-model-map [model-map-values {:keys [skip-model-map-stringify?] :as options}]
|
(defn- make-model-map [model-map-values {:keys [skip-model-map-stringify?] :as options}]
|
||||||
(let [model-map-obj (new JtwigModelMap)
|
(let [model-map-obj (new JtwigModelMap)
|
||||||
|
@ -165,7 +167,7 @@
|
||||||
|
|
||||||
(defn render-file
|
(defn render-file
|
||||||
"renders a template from a file, using the values in model-map as the model for the template"
|
"renders a template from a file, using the values in model-map as the model for the template"
|
||||||
[filename model-map & [options]]
|
[^String filename model-map & [options]]
|
||||||
(let [file (new File filename)
|
(let [file (new File filename)
|
||||||
compiled-template (compile-template! file)]
|
compiled-template (compile-template! file)]
|
||||||
(render-compiled-template compiled-template model-map options)))
|
(render-compiled-template compiled-template model-map options)))
|
||||||
|
@ -173,6 +175,6 @@
|
||||||
(defn render-resource
|
(defn render-resource
|
||||||
"renders a template from a resource file, using the values in the model-map as the model for
|
"renders a template from a resource file, using the values in the model-map as the model for
|
||||||
the template."
|
the template."
|
||||||
[filename model-map & [options]]
|
[^String filename model-map & [options]]
|
||||||
(if-let [resource-filename (get-resource-path filename)]
|
(if-let [resource-filename (get-resource-path filename)]
|
||||||
(render-file (.getPath resource-filename) model-map options)))
|
(render-file (.getPath resource-filename) model-map options)))
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
(ns clj-jtwig.functions
|
(ns clj-jtwig.functions
|
||||||
"standard functions added to jtwig contexts by default. these are in addition to the
|
"custom template function/filter support functions."
|
||||||
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 FunctionException))
|
(com.lyncode.jtwig.functions.exceptions FunctionNotFoundException FunctionException))
|
||||||
|
@ -41,7 +40,7 @@
|
||||||
[]
|
[]
|
||||||
(reset! functions (create-function-repository)))
|
(reset! functions (create-function-repository)))
|
||||||
|
|
||||||
(defn function-exists? [name]
|
(defn function-exists? [^String name]
|
||||||
(try
|
(try
|
||||||
(.retrieve @functions name)
|
(.retrieve @functions name)
|
||||||
true
|
true
|
||||||
|
@ -52,9 +51,9 @@
|
||||||
"adds a new template function under the name specified. templates can call the function by the
|
"adds a new template function under the name specified. templates can call the function by the
|
||||||
name specified (or one of the aliases specified) and passing in the same number of arguments
|
name specified (or one of the aliases specified) and passing in the same number of arguments
|
||||||
accepted by f. the return value of f is returned to the template."
|
accepted by f. the return value of f is returned to the template."
|
||||||
([name f]
|
([^String name f]
|
||||||
(add-function! name nil f))
|
(add-function! name nil f))
|
||||||
([name aliases f]
|
([^String name aliases f]
|
||||||
(let [handler (make-function-handler f)]
|
(let [handler (make-function-handler f)]
|
||||||
(.add @functions handler name (make-aliased-array aliases))
|
(.add @functions handler name (make-aliased-array aliases))
|
||||||
(.retrieve @functions name))))
|
(.retrieve @functions name))))
|
||||||
|
|
|
@ -13,7 +13,8 @@
|
||||||
(defonce standard-functions
|
(defonce standard-functions
|
||||||
{"blankIfNull"
|
{"blankIfNull"
|
||||||
{:fn (fn [x]
|
{:fn (fn [x]
|
||||||
(if (nil? x) "" x))}
|
(if (nil? x) "" x))
|
||||||
|
:aliases ["nonull"]}
|
||||||
|
|
||||||
"butlast"
|
"butlast"
|
||||||
{:fn (fn [sequence]
|
{:fn (fn [sequence]
|
||||||
|
|
|
@ -18,14 +18,14 @@
|
||||||
|
|
||||||
(is (valid-function-handler?
|
(is (valid-function-handler?
|
||||||
(deftwigfn "add" [a b]
|
(deftwigfn "add" [a b]
|
||||||
(+ a b))))
|
(+ a b))))
|
||||||
|
|
||||||
(is (true? (function-exists? "add")))
|
(is (true? (function-exists? "add")))
|
||||||
(is (false? (function-exists? "foobar")))
|
(is (false? (function-exists? "foobar")))
|
||||||
|
|
||||||
(is (valid-function-handler?
|
(is (valid-function-handler?
|
||||||
(deftwigfn "add" [a b]
|
(deftwigfn "add" [a b]
|
||||||
(+ a b))))
|
(+ a b))))
|
||||||
|
|
||||||
(is (= (render "{{add(1, 2)}}" nil)
|
(is (= (render "{{add(1, 2)}}" nil)
|
||||||
"3")
|
"3")
|
||||||
|
@ -39,17 +39,42 @@
|
||||||
|
|
||||||
(reset-functions!)))
|
(reset-functions!)))
|
||||||
|
|
||||||
|
(testing "Custom template function aliases"
|
||||||
|
(do
|
||||||
|
(reset-functions!)
|
||||||
|
|
||||||
|
(is (valid-function-handler?
|
||||||
|
(defaliasedtwigfn "add" [a b]
|
||||||
|
["plus" "myAddFn"]
|
||||||
|
(+ a b))))
|
||||||
|
|
||||||
|
(is (true? (function-exists? "add")))
|
||||||
|
(is (true? (function-exists? "plus")))
|
||||||
|
(is (true? (function-exists? "myAddFn")))
|
||||||
|
|
||||||
|
(is (= (render "{{add(1, 2)}}" nil)
|
||||||
|
"3")
|
||||||
|
"calling a custom function by name")
|
||||||
|
(is (= (render "{{plus(1, 2)}}" nil)
|
||||||
|
"3")
|
||||||
|
"calling a custom function by alias")
|
||||||
|
(is (= (render "{{myAddFn(1, 2)}}" nil)
|
||||||
|
"3")
|
||||||
|
"calling a custom function by another alias")
|
||||||
|
|
||||||
|
(reset-functions!)))
|
||||||
|
|
||||||
(testing "Fixed and variable number of template function arguments"
|
(testing "Fixed and variable number of template function arguments"
|
||||||
(do
|
(do
|
||||||
(reset-functions!)
|
(reset-functions!)
|
||||||
|
|
||||||
(is (valid-function-handler?
|
(is (valid-function-handler?
|
||||||
(deftwigfn "add2" [a b]
|
(deftwigfn "add2" [a b]
|
||||||
(+ a b))))
|
(+ a b))))
|
||||||
(is (true? (function-exists? "add2")))
|
(is (true? (function-exists? "add2")))
|
||||||
(is (valid-function-handler?
|
(is (valid-function-handler?
|
||||||
(deftwigfn "addAll" [& numbers]
|
(deftwigfn "addAll" [& numbers]
|
||||||
(apply + numbers))))
|
(apply + numbers))))
|
||||||
(is (true? (function-exists? "addAll")))
|
(is (true? (function-exists? "addAll")))
|
||||||
|
|
||||||
(is (= (render "{{add2(1, 2)}}" nil)
|
(is (= (render "{{add2(1, 2)}}" nil)
|
||||||
|
@ -74,11 +99,11 @@
|
||||||
|
|
||||||
(is (valid-function-handler?
|
(is (valid-function-handler?
|
||||||
(deftwigfn "identity" [x]
|
(deftwigfn "identity" [x]
|
||||||
x)))
|
x)))
|
||||||
(is (true? (function-exists? "identity")))
|
(is (true? (function-exists? "identity")))
|
||||||
(is (valid-function-handler?
|
(is (valid-function-handler?
|
||||||
(deftwigfn "typename" [x]
|
(deftwigfn "typename" [x]
|
||||||
(.getName (type x)))))
|
(.getName (type x)))))
|
||||||
(is (true? (function-exists? "typename")))
|
(is (true? (function-exists? "typename")))
|
||||||
|
|
||||||
; verify that the clojure function recognizes the correct types when the variable is passed via the model-map
|
; verify that the clojure function recognizes the correct types when the variable is passed via the model-map
|
||||||
|
@ -250,7 +275,9 @@
|
||||||
(is (= (render "{{ a|blankIfNull }}" {:a nil})
|
(is (= (render "{{ a|blankIfNull }}" {:a nil})
|
||||||
""))
|
""))
|
||||||
(is (= (render "{{ a|blankIfNull }}" {:a "foo"})
|
(is (= (render "{{ a|blankIfNull }}" {:a "foo"})
|
||||||
"foo")))
|
"foo"))
|
||||||
|
(is (= (render "{{ a|nonull }}" nil)
|
||||||
|
"")))
|
||||||
|
|
||||||
(testing "butlast"
|
(testing "butlast"
|
||||||
(is (= (render "{{ [1, 2, 3, 4, 5]|butlast }}" nil)
|
(is (= (render "{{ [1, 2, 3, 4, 5]|butlast }}" nil)
|
||||||
|
|
Reference in a new issue