Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created April 8, 2010 07:51
Show Gist options
  • Save cgrand/359887 to your computer and use it in GitHub Desktop.
Save cgrand/359887 to your computer and use it in GitHub Desktop.
;; answer to what I understood of http://d.hatena.ne.jp/fatrow/20100407/1270662784
;; your original code
(en/defsnippet lib-model "src/html/a.html" [:.library]
[{:keys [name description url category]}]
[:.name] (en/content name)
[:.descriptionj] (en/content description)
[:.site :a] (en/do-> (en/set-attr :href url)
(en/content url))
(en/deftemplate libpage "src/html/a.html"
[libdata]
[:.library] (en/substitute (map lib-model libdata)))
;; rewritten with clone-for:
(en/deftemplate libpage "src/html/a.html"
[libdata]
[:.library] (clone-for [{:keys [name description url category]} lib-data]
[:.name] (en/content name)
[:.descriptionj] (en/content description)
[:.site :a] (en/do-> (en/set-attr :href url)
(en/content url))))
;; clone-for first vector is a seq comprehension (like in #'for) and its body consists of pairs of selectors/transformations
;; so clone-for clones the elements selected by [:.library] as many times there are items in lib-data and then each clone is
;; transformed according to the selectors/transformations specified as body of the clone-for form.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment