From fdcd0d74d3215682f9c7b25a47a082c9408702ff Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 4 Mar 2014 14:37:48 -0500 Subject: [PATCH 1/6] add alias for "blankIfNull" --- src/clj_jtwig/standard_functions.clj | 3 ++- test/clj_jtwig/functions_test.clj | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/clj_jtwig/standard_functions.clj b/src/clj_jtwig/standard_functions.clj index 1d591d9..a4a034f 100644 --- a/src/clj_jtwig/standard_functions.clj +++ b/src/clj_jtwig/standard_functions.clj @@ -13,7 +13,8 @@ (defonce standard-functions {"blankIfNull" {:fn (fn [x] - (if (nil? x) "" x))} + (if (nil? x) "" x)) + :aliases ["nonull"]} "butlast" {:fn (fn [sequence] diff --git a/test/clj_jtwig/functions_test.clj b/test/clj_jtwig/functions_test.clj index 1aa1db8..09be3ba 100644 --- a/test/clj_jtwig/functions_test.clj +++ b/test/clj_jtwig/functions_test.clj @@ -250,7 +250,9 @@ (is (= (render "{{ a|blankIfNull }}" {:a nil}) "")) (is (= (render "{{ a|blankIfNull }}" {:a "foo"}) - "foo"))) + "foo")) + (is (= (render "{{ a|nonull }}" nil) + ""))) (testing "butlast" (is (= (render "{{ [1, 2, 3, 4, 5]|butlast }}" nil) From 0624425e614621438478866cb8a89cfc10e9f4f4 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 4 Mar 2014 14:41:46 -0500 Subject: [PATCH 2/6] add tests for template function aliases --- test/clj_jtwig/functions_test.clj | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/clj_jtwig/functions_test.clj b/test/clj_jtwig/functions_test.clj index 09be3ba..a7a9152 100644 --- a/test/clj_jtwig/functions_test.clj +++ b/test/clj_jtwig/functions_test.clj @@ -39,6 +39,31 @@ (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" (do (reset-functions!) From 3cca389e8b4ae5f36f773169a3e409b08a8062ef Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 4 Mar 2014 14:42:52 -0500 Subject: [PATCH 3/6] formatting --- test/clj_jtwig/functions_test.clj | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/clj_jtwig/functions_test.clj b/test/clj_jtwig/functions_test.clj index a7a9152..7fc9342 100644 --- a/test/clj_jtwig/functions_test.clj +++ b/test/clj_jtwig/functions_test.clj @@ -18,14 +18,14 @@ (is (valid-function-handler? (deftwigfn "add" [a b] - (+ a b)))) + (+ a b)))) (is (true? (function-exists? "add"))) (is (false? (function-exists? "foobar"))) (is (valid-function-handler? (deftwigfn "add" [a b] - (+ a b)))) + (+ a b)))) (is (= (render "{{add(1, 2)}}" nil) "3") @@ -70,11 +70,11 @@ (is (valid-function-handler? (deftwigfn "add2" [a b] - (+ a b)))) + (+ a b)))) (is (true? (function-exists? "add2"))) (is (valid-function-handler? (deftwigfn "addAll" [& numbers] - (apply + numbers)))) + (apply + numbers)))) (is (true? (function-exists? "addAll"))) (is (= (render "{{add2(1, 2)}}" nil) @@ -99,11 +99,11 @@ (is (valid-function-handler? (deftwigfn "identity" [x] - x))) + x))) (is (true? (function-exists? "identity"))) (is (valid-function-handler? (deftwigfn "typename" [x] - (.getName (type x))))) + (.getName (type x))))) (is (true? (function-exists? "typename"))) ; verify that the clojure function recognizes the correct types when the variable is passed via the model-map From d4ec61b306b19b4995fe0ac7a1df2425b2905d38 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 4 Mar 2014 16:42:44 -0500 Subject: [PATCH 4/6] add more type hints --- src/clj_jtwig/core.clj | 16 +++++++++------- src/clj_jtwig/functions.clj | 6 +++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/clj_jtwig/core.clj b/src/clj_jtwig/core.clj index fe29a83..2655bc3 100644 --- a/src/clj_jtwig/core.clj +++ b/src/clj_jtwig/core.clj @@ -2,7 +2,8 @@ "wrapper functions for working with JTwig from clojure" (:import (com.lyncode.jtwig JtwigTemplate JtwigContext JtwigModelMap) (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]]) (:use [clj-jtwig.functions])) @@ -128,10 +129,11 @@ [] (reset! compiled-templates {})) -(defn- get-resource-path [filename] - (-> (Thread/currentThread) - (.getContextClassLoader) - (.getResource filename))) +(defn- get-resource-path + (^URL [^String filename] + (-> (Thread/currentThread) + (.getContextClassLoader) + (.getResource filename)))) (defn- make-model-map [model-map-values {:keys [skip-model-map-stringify?] :as options}] (let [model-map-obj (new JtwigModelMap) @@ -165,7 +167,7 @@ (defn render-file "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) compiled-template (compile-template! file)] (render-compiled-template compiled-template model-map options))) @@ -173,6 +175,6 @@ (defn render-resource "renders a template from a resource file, using the values in the model-map as the model for the template." - [filename model-map & [options]] + [^String filename model-map & [options]] (if-let [resource-filename (get-resource-path filename)] (render-file (.getPath resource-filename) model-map options))) diff --git a/src/clj_jtwig/functions.clj b/src/clj_jtwig/functions.clj index 56562eb..426af78 100644 --- a/src/clj_jtwig/functions.clj +++ b/src/clj_jtwig/functions.clj @@ -41,7 +41,7 @@ [] (reset! functions (create-function-repository))) -(defn function-exists? [name] +(defn function-exists? [^String name] (try (.retrieve @functions name) true @@ -52,9 +52,9 @@ "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 accepted by f. the return value of f is returned to the template." - ([name f] + ([^String name f] (add-function! name nil f)) - ([name aliases f] + ([^String name aliases f] (let [handler (make-function-handler f)] (.add @functions handler name (make-aliased-array aliases)) (.retrieve @functions name)))) From 8d6eba99b07b7a0f13d15973e1ecb6412316bbb6 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 4 Mar 2014 16:46:00 -0500 Subject: [PATCH 5/6] update namespace doc comment --- src/clj_jtwig/functions.clj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/clj_jtwig/functions.clj b/src/clj_jtwig/functions.clj index 426af78..44a77f7 100644 --- a/src/clj_jtwig/functions.clj +++ b/src/clj_jtwig/functions.clj @@ -1,6 +1,5 @@ (ns clj-jtwig.functions - "standard functions added to jtwig contexts by default. these are in addition to the - functions added by default in all jtwig function repository objects" + "custom template function/filter support functions." (:import (com.lyncode.jtwig.functions JtwigFunction) (com.lyncode.jtwig.functions.repository DefaultFunctionRepository) (com.lyncode.jtwig.functions.exceptions FunctionNotFoundException FunctionException)) From 17caf40f2a5147d8e269b5c8376bad018d842752 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 4 Mar 2014 16:55:36 -0500 Subject: [PATCH 6/6] bump version --- project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.clj b/project.clj index 43514b6..7527a2d 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject clj-jtwig "0.2" +(defproject clj-jtwig "0.2.1" :description "Clojure wrapper for JTwig" :url "https://github.com/gered/clj-jtwig" :license {:name "Apache License, Version 2.0"