add :tag-symbols option

This commit is contained in:
Gered 2014-07-02 17:05:52 -04:00
parent 5a644a92d2
commit ba78333c28
3 changed files with 30 additions and 3 deletions

View file

@ -3,6 +3,7 @@
(:import (com.lyncode.jtwig JtwigTemplate JtwigContext JtwigModelMap) (:import (com.lyncode.jtwig JtwigTemplate JtwigContext JtwigModelMap)
(com.lyncode.jtwig.content.api Renderable) (com.lyncode.jtwig.content.api Renderable)
(com.lyncode.jtwig.configuration JtwigConfiguration) (com.lyncode.jtwig.configuration JtwigConfiguration)
(com.lyncode.jtwig.parser.config TagSymbols)
(com.lyncode.jtwig.render RenderContext) (com.lyncode.jtwig.render RenderContext)
(com.lyncode.jtwig.render.config RenderConfiguration) (com.lyncode.jtwig.render.config RenderConfiguration)
(com.lyncode.jtwig.resource ClasspathJtwigResource StringJtwigResource FileJtwigResource) (com.lyncode.jtwig.resource ClasspathJtwigResource StringJtwigResource FileJtwigResource)
@ -31,7 +32,14 @@
(flush-template-cache!) (flush-template-cache!)
(= k :strict-mode) (= k :strict-mode)
(-> configuration .render (.strictMode v))) (-> configuration .render (.strictMode v))
(= k :tag-symbols)
(-> configuration
.parse
(.withSymbols (condp = v
:default TagSymbols/DEFAULT
:js TagSymbols/JAVASCRIPT))))
(swap! options assoc k v))) (swap! options assoc k v)))

View file

@ -44,4 +44,10 @@
:web-resource-path-root "public" :web-resource-path-root "public"
; when enabled, exceptions will be thrown when attempting to use variables that do not exist ; when enabled, exceptions will be thrown when attempting to use variables that do not exist
:strict-mode false})) :strict-mode false
; allows for changing the tag symbols used from normal Twig-style to a style more friendly
; with certain Javascript template engines (e.g. Angular).
; :default => tag: {% %}, output: {{ }}, comment: {# #}
; :js => tag: <# #>, output: <@ @>, comment: <$ $>
:tag-symbols :default}))

View file

@ -156,4 +156,17 @@
"trying to output a non-existant variable under non-strict-mode") "trying to output a non-existant variable under non-strict-mode")
(is (= (render "{{ foo }}" {:foo "bar"}) (is (= (render "{{ foo }}" {:foo "bar"})
"bar") "bar")
"trying to output an existing variable under non-strict-mode")))) "trying to output an existing variable under non-strict-mode"))
(do
(set-options! :tag-symbols :js)
(is (= (render "@> 1 <@" {:foo "bar"})
"1")
"js-style output symbols")
(is (= (render "<# if (foo) #>bar<# endif #>" {:foo true})
"bar")
"js-style tag symbols")
(is (= (render "<$ this is a comment $>")
"")
"js-style comment symbols")
(set-options! :tag-symbols :default))))