add toggle control to turn template caching on/off

This commit is contained in:
Gered 2014-03-02 14:28:39 -05:00
parent c64a826a12
commit e1b53036ed

View file

@ -12,6 +12,12 @@
; global options ; global options
(defonce options (atom {:cache-compiled-templates true})) (defonce options (atom {:cache-compiled-templates true}))
(defn toggle-compiled-template-caching!
"toggle caching of compiled templates on/off. if off, every time a template is rendered from a file
it will be re-loaded from disk and re-compiled before being rendered. caching is turned on by default."
[enable?]
(swap! options assoc :cache-compiled-templates enable?))
; cache of compiled templates. key is the file path. value is a map with :last-modified which is the source file's ; cache of compiled templates. key is the file path. value is a map with :last-modified which is the source file's
; last modification timestamp and :template which is a JTwig Content object which has been compiled already and can ; last modification timestamp and :template which is a JTwig Content object which has been compiled already and can
; be rendered by calling it's 'render' method ; be rendered by calling it's 'render' method
@ -68,10 +74,12 @@
(defn- compile-template! [^File file] (defn- compile-template! [^File file]
(if-not (.exists file) (if-not (.exists file)
(throw (new FileNotFoundException (str "Template file \"" file "\" not found."))) (throw (new FileNotFoundException (str "Template file \"" file "\" not found.")))
(cache-compiled-template! (let [compile-template-fn (fn [file]
file (println "compiling template: " file)
(fn [file] (compile-template-file file))]
(compile-template-file file))))) (if (:cache-compiled-templates @options)
(cache-compiled-template! file compile-template-fn)
(compile-template-fn file)))))
(defn flush-template-cache! (defn flush-template-cache!
"removes any cached compiled templates, forcing all future template rendering to first re-compile before "removes any cached compiled templates, forcing all future template rendering to first re-compile before