Last active
January 1, 2016 22:19
-
-
Save knjname/8208883 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (def var-pattern #"\$\{(.+?)\}") | |
| (defn compile-var-subst-only-template-fn | |
| ([template data-symbol] | |
| (compile-var-subst-only-template-fn template data-symbol 0 (gensym))) | |
| ([template data-symbol start-pos sb-symbol] | |
| `(let [~sb-symbol (StringBuilder. )] | |
| ~@(loop [m (re-matcher var-pattern template) | |
| last-pos 0 | |
| expr-list []] | |
| (if (.find m) | |
| (recur m | |
| (.end m) | |
| (conj expr-list | |
| `(.append ~sb-symbol ~(.substring template last-pos (.start m))) | |
| `(.append ~sb-symbol (get ~data-symbol ~(.group m 1))))) | |
| (conj expr-list | |
| `(.append ~sb-symbol ~(.substring template last-pos))))) | |
| (.toString ~sb-symbol)))) | |
| (defmacro compile-var-subst-only-template [template data] | |
| (compile-var-subst-only-template-fn template data)) | |
| (defn greeting [client-user] | |
| (compile-var-subst-only-template | |
| "Dear ${firstname} ${lastname}. Welcome to our store." client-user)) | |
| ;; => To be: | |
| ;; (def greeting | |
| ;; (fn* | |
| ;; ([client-user] | |
| ;; (let* | |
| ;; [G__11086 (new java.lang.StringBuilder)] | |
| ;; (. G__11086 append "Dear ") | |
| ;; (. G__11086 append (clojure.core/get client-user "firstname")) | |
| ;; (. G__11086 append " ") | |
| ;; (. G__11086 append (clojure.core/get client-user "lastname")) | |
| ;; (. G__11086 append ". Welcome to our store.") | |
| ;; (. G__11086 toString))))) | |
| (greeting {"firstname" "John" "lastname" "Smith"}) |
Author
Author
I think it would be nice if "(get data var-name)" expression were specifiable from client API.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've reproduced something like JSP.