Skip to content

Instantly share code, notes, and snippets.

@niwinz
Forked from rauhs/sablono-hacks.clj
Created April 8, 2017 15:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niwinz/fb7eff120b7033d0854fee5fb6b7ee9c to your computer and use it in GitHub Desktop.
Save niwinz/fb7eff120b7033d0854fee5fb6b7ee9c to your computer and use it in GitHub Desktop.
(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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment