Created
October 21, 2016 17:42
-
-
Save rauhs/38b8598c6549f2fe09ad4d257382ec32 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
(ns your.macros-for-cljs.ns | |
(:require [sablono.compiler :as sablono-c])) | |
;; Make sablono also walk into other forms: | |
;; if, for, let, do: Already exist | |
(.addMethod @(var sablono-c/compile-form) "when" | |
(fn | |
[[_ bindings & body]] | |
`(when ~bindings ~@(for [x body] (sablono-c/compile-html x))))) | |
(.addMethod @(var sablono-c/compile-form) "when-not" | |
(fn | |
[[_ bindings & body]] | |
`(when-not ~bindings ~@(for [x body] (sablono-c/compile-html x))))) | |
(.addMethod @(var sablono-c/compile-form) "if-not" | |
(fn | |
[[_ bindings & body]] | |
`(if-not ~bindings ~@(for [x body] (sablono-c/compile-html x))))) | |
;; Don't interpret by default: User will have to treat errors instead: | |
(.addMethod @(var sablono-c/compile-form) :default | |
(fn | |
[expr] | |
`~expr)) | |
;; This is for vectors: | |
;; (html [:div "foo" [[:span "hi"] [:span "more"]]] | |
(.addMethod @(var sablono-c/compile-element) :default | |
(fn [[el & r :as elements]] | |
(mapv sablono-c/compile-html elements))) |
(.addMethod @(var sablono-c/compile-form) "cond"
(fn [[_ & clauses]]
`(cond ~@(mapcat
(fn [[check expr]] [check (sablono.compiler/compile-html expr)])
(partition 2 clauses)))))
(.addMethod @(var sablono-c/compile-form) "case"
(fn [[_ var & clauses]]
`(case ~var ~@(mapcat
(fn [[constant expr]]
(if expr
[constant (sablono.compiler/compile-html expr)]
;; default expression
[(sablono.compiler/compile-html constant)]))
(partition 2 clauses)))))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this gist! :)