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:
Gered 2014-06-13 15:23:42 -04:00
parent 291842af48
commit 841b15e11d
2 changed files with 30 additions and 13 deletions

View file

@ -37,6 +37,13 @@
[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
; 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
@ -117,22 +124,22 @@
[]
(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)
values (if-not skip-model-map-stringify?
values (if (:stringify-keys @options)
(stringify-keys model-map-values)
model-map-values)]
(doseq [[k v] values]
(.add model-map-obj k v))
model-map-obj))
(defn- make-context [model-map options]
(let [model-map-obj (make-model-map model-map options)]
(defn- make-context [model-map]
(let [model-map-obj (make-model-map model-map)]
(new JtwigContext model-map-obj @functions)))
(defn- render-compiled-template
[^Content compiled-template model-map & [options]]
(let [context (make-context model-map options)]
[^Content compiled-template model-map]
(let [context (make-context model-map)]
; 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)]
@ -143,21 +150,21 @@
"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
parsed, compiled and rendered. the compiled results are never cached."
[s model-map & [options]]
[s model-map]
(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
"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)
compiled-template (compile-template! file)]
(render-compiled-template compiled-template model-map options)))
(render-compiled-template compiled-template model-map)))
(defn render-resource
"renders a template from a resource file, using the values in the model-map as the model for
the template."
[^String filename model-map & [options]]
[^String filename model-map]
(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.")))))

View file

@ -28,4 +28,14 @@
; 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
; 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}))