replace old 'skip-model-map-stringify?' option with a new global option
as well, by default now, keys will NOT be automatically stringified. the user must turn this behaviour on explicitly. this is to reduce the level of "magic" that happens behind the scenes (if there was a 100% fool- proof method of stringifying/keywordizing back and forth as needed without ever being an issue i would turn this behaviour on all the time)
This commit is contained in:
parent
291842af48
commit
841b15e11d
|
@ -37,6 +37,13 @@
|
||||||
[enable?]
|
[enable?]
|
||||||
(swap! options assoc :check-for-minified-web-resources enable?))
|
(swap! options assoc :check-for-minified-web-resources enable?))
|
||||||
|
|
||||||
|
(defn toggle-auto-stringify-keys!
|
||||||
|
[enable?]
|
||||||
|
"toggle whether model-maps passed to the render functions will have their keys recursively converted
|
||||||
|
from keywords to strings. Jtwig requires that all the keys in model maps are strings. this is turned
|
||||||
|
off by default."
|
||||||
|
(swap! options assoc :stringify-keys 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 com.lyncode.jtwig.tree.api.Content object which has been
|
; last modification timestamp and :template which is a com.lyncode.jtwig.tree.api.Content object which has been
|
||||||
; compiled already and can be rendered by calling it's 'render' method
|
; compiled already and can be rendered by calling it's 'render' method
|
||||||
|
@ -117,22 +124,22 @@
|
||||||
[]
|
[]
|
||||||
(reset! compiled-templates {}))
|
(reset! compiled-templates {}))
|
||||||
|
|
||||||
(defn- make-model-map [model-map-values {:keys [skip-model-map-stringify?] :as options}]
|
(defn- make-model-map [model-map-values]
|
||||||
(let [model-map-obj (new JtwigModelMap)
|
(let [model-map-obj (new JtwigModelMap)
|
||||||
values (if-not skip-model-map-stringify?
|
values (if (:stringify-keys @options)
|
||||||
(stringify-keys model-map-values)
|
(stringify-keys model-map-values)
|
||||||
model-map-values)]
|
model-map-values)]
|
||||||
(doseq [[k v] values]
|
(doseq [[k v] values]
|
||||||
(.add model-map-obj k v))
|
(.add model-map-obj k v))
|
||||||
model-map-obj))
|
model-map-obj))
|
||||||
|
|
||||||
(defn- make-context [model-map options]
|
(defn- make-context [model-map]
|
||||||
(let [model-map-obj (make-model-map model-map options)]
|
(let [model-map-obj (make-model-map model-map)]
|
||||||
(new JtwigContext model-map-obj @functions)))
|
(new JtwigContext model-map-obj @functions)))
|
||||||
|
|
||||||
(defn- render-compiled-template
|
(defn- render-compiled-template
|
||||||
[^Content compiled-template model-map & [options]]
|
[^Content compiled-template model-map]
|
||||||
(let [context (make-context model-map options)]
|
(let [context (make-context model-map)]
|
||||||
; technically we don't have to use with-open with a ByteArrayOutputStream but if we later
|
; 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 :)
|
; decide to use another OutputStream implementation, this is already all set up :)
|
||||||
(with-open [stream (new ByteArrayOutputStream)]
|
(with-open [stream (new ByteArrayOutputStream)]
|
||||||
|
@ -143,21 +150,21 @@
|
||||||
"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. templates rendered using this function are always
|
as the model for the template. templates rendered using this function are always
|
||||||
parsed, compiled and rendered. the compiled results are never cached."
|
parsed, compiled and rendered. the compiled results are never cached."
|
||||||
[s model-map & [options]]
|
[s model-map]
|
||||||
(let [compiled-template (compile-template-string s)]
|
(let [compiled-template (compile-template-string s)]
|
||||||
(render-compiled-template compiled-template model-map options)))
|
(render-compiled-template compiled-template model-map)))
|
||||||
|
|
||||||
(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"
|
||||||
[^String filename model-map & [options]]
|
[^String filename model-map]
|
||||||
(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)))
|
||||||
|
|
||||||
(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."
|
||||||
[^String filename model-map & [options]]
|
[^String filename model-map]
|
||||||
(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)
|
||||||
(throw (new FileNotFoundException (str "Template file \"" filename "\" not found.")))))
|
(throw (new FileNotFoundException (str "Template file \"" filename "\" not found.")))))
|
||||||
|
|
|
@ -28,4 +28,14 @@
|
||||||
; part of the filename, and if so use that file instead.
|
; part of the filename, and if so use that file instead.
|
||||||
; note that enabling this option does obviously incur a slight file I/O performance penalty
|
; note that enabling this option does obviously incur a slight file I/O performance penalty
|
||||||
; whenever these functions are used
|
; whenever these functions are used
|
||||||
:check-for-minified-web-resources true}))
|
:check-for-minified-web-resources true
|
||||||
|
|
||||||
|
; whether or not to automatically stringify the keys of model-maps. Jtwig requires that all
|
||||||
|
; the keys will be strings for model value resolution to work correctly. if you are already
|
||||||
|
; setting your keys as maps, then you can turn this option off to save a bit on performance
|
||||||
|
; NOTE: why use false as a default? less "magic" out of the box. most people when first
|
||||||
|
; using this library will probably run into this issue and then want to turn this
|
||||||
|
; option on, but i think that's preferable (forcing the user to learn about this)
|
||||||
|
; then later on wondering why the value from the model passed to a custom jtwig
|
||||||
|
; function has strings for keys instead of keywords, etc...
|
||||||
|
:stringify-keys false}))
|
Reference in a new issue