Frage

I'm trying to find an 'Enlivonic' way of defining a function that will perform a transformation on a parameterised html template.

In other words, how do I define something like a defsnippet that also takes the template as an argument?

I looked at at, snippet and transformation, but I'm a little lost in the macros :-(

War es hilfreich?

Lösung

If you have a transformation, but want to feed it different templates, you could try:

(defn multi-template [t]
    (template t [h1 h2]
         [:h1] (content h1)
         [:h2] (content h2)))

This function, when called with an html template will return a function that takes h1 and h2 as arguments and substitutes them into the given template. Call it as follows:

((multi-template "your-template.html") "heading 1 content" "heading 2 content")

I used something similar to this when I had a number of different picture gallery designs as templates and wanted to switch between dynamically.

Note: this dynamically creates the template each time, whereas the deftemplate macro just calls template once at compile time. So if this is too slow, there are things you can do to optimise it (see clojure, enlive, multi-site)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top