update render-* functions to use compiled template objects
This commit is contained in:
parent
65109bccc5
commit
128a84dfcb
|
@ -6,7 +6,7 @@
|
||||||
(com.lyncode.jtwig.functions.exceptions FunctionNotFoundException)
|
(com.lyncode.jtwig.functions.exceptions FunctionNotFoundException)
|
||||||
(com.lyncode.jtwig.functions.repository DefaultFunctionRepository)
|
(com.lyncode.jtwig.functions.repository DefaultFunctionRepository)
|
||||||
(com.lyncode.jtwig.functions JtwigFunction)
|
(com.lyncode.jtwig.functions JtwigFunction)
|
||||||
(java.io File FileNotFoundException)))
|
(java.io File FileNotFoundException ByteArrayOutputStream)))
|
||||||
|
|
||||||
; 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
|
||||||
|
@ -87,25 +87,28 @@
|
||||||
(let [model-map-obj (make-model-map model-map options)]
|
(let [model-map-obj (make-model-map model-map options)]
|
||||||
(new JtwigContext model-map-obj @functions)))
|
(new JtwigContext model-map-obj @functions)))
|
||||||
|
|
||||||
(defn- render-template
|
(defn- render-compiled-template
|
||||||
[template model-map & [options]]
|
[compiled-template model-map & [options]]
|
||||||
(.output template (make-context model-map options)))
|
(let [context (make-context model-map options)]
|
||||||
|
; technically we don't have to use with-open with a ByteArrayOutputStream but if we later
|
||||||
|
; decide to use another OutputStream implementation, this is already all set up :)
|
||||||
|
(with-open [stream (new ByteArrayOutputStream)]
|
||||||
|
(.render compiled-template stream context)
|
||||||
|
(.toString stream))))
|
||||||
|
|
||||||
(defn render
|
(defn render
|
||||||
"renders a template contained in the provided string, using the values in model-map
|
"renders a template contained in the provided string, using the values in model-map
|
||||||
as the model for the template."
|
as the model for the template."
|
||||||
[s model-map & [options]]
|
[s model-map & [options]]
|
||||||
(let [template (new JtwigTemplate s)]
|
(let [compiled-template (compile-template-string s)]
|
||||||
(render-template template model-map options)))
|
(render-compiled-template compiled-template model-map options)))
|
||||||
|
|
||||||
(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]]
|
[filename model-map & [options]]
|
||||||
(let [file (new File filename)
|
(let [file (new File filename)
|
||||||
template (new JtwigTemplate file)]
|
compiled-template (get-compiled-template file)]
|
||||||
(if-not (.exists file)
|
(render-compiled-template compiled-template model-map options)))
|
||||||
(throw (new FileNotFoundException (str "Template file \"" filename "\" not found."))))
|
|
||||||
(render-template template model-map options)))
|
|
||||||
|
|
||||||
(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
|
||||||
|
|
Reference in a new issue