0

I have some transformations in enlive:

(html/deftemplate tranforma-numero-template "public/index.html" [m]
              [:#portugues :.conteudo] (html/content (:portugues m))
              [:#ingles :.conteudo] (html/content (:ingles m))
              [:#espanhol :.conteudo] (html/content (:espanhol m)))

It follows a pattern. So I would like to abstract that pattern in some way. I imagine if something like this would be possible:

(html/deftemplate tranforma-numero-template "public/index.html" [m]
    [(html/pred (fn [node]
        (when (seq formatos)
           (-> node :attrs :id (set formatos)))))] (html/content ((keyword (-> node :attrs :id)) m))) 

I know this is overkill for such a simple example, but you got the concept.

Carlos Nunes
  • 1,929
  • 2
  • 16
  • 20

1 Answers1

0

One alternative to writing a general selector, as you have nicely done above, is to dynamically generate the call to deftemplate. Writing a smart selector and transformer like in your example is a perfetly fine approach as well.

Because deftemplate is a macro, it will likely be easier if your template creation code is also be a macro. This macro contagion is one downside to making macros the primary interface to a library. Unfortunatly it means that this will be more diffacult. Fortuneatly the macro to produce a call to deftemplate is even simpler than the smart selector approach. It's just a matter of producing a call to deftemplate with the appropriate sequence of selectors and transformations.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284