add support for adding custom template filters

This commit is contained in:
Gered 2014-10-05 15:25:23 -04:00
parent 550550a210
commit bf68239b90

View file

@ -4,7 +4,7 @@
(com.mitchellbosecke.pebble PebbleEngine)
(com.mitchellbosecke.pebble.loader ClasspathLoader FileLoader StringLoader)
(com.mitchellbosecke.pebble.template PebbleTemplate)
(com.mitchellbosecke.pebble.extension Function))
(com.mitchellbosecke.pebble.extension Function Filter))
(:require [clojure.walk :refer [stringify-keys]]))
(defonce classpath-loader (ClasspathLoader.))
@ -33,11 +33,22 @@
(execute [_ args]
(apply f (get-sorted-args args)))))
(defn make-filter [f]
(reify Filter
(getArgumentNames [_] nil)
(apply [_ input args]
(apply f (concat [input] (get-sorted-args args))))))
(defmacro defpebblefn [fn-name args & body]
`(let [f# (fn ~args ~@body)
pebble-fn# (make-function f#)]
(.put (.getFunctions @engine) ~fn-name pebble-fn#)))
(defmacro defpebblefilter [filter-name args & body]
`(let [f# (fn ~args ~@body)
pebble-fn# (make-filter f#)]
(.put (.getFilters @engine) ~filter-name pebble-fn#)))
(defn- prepare-context-map [context]
(if context
(stringify-keys context)