CL-WHO-like HTML templating for other languages?

前端 未结 9 1076
别那么骄傲
别那么骄傲 2020-12-10 04:44

Common Lisp guys have their CL-WHO, which makes HTML templating integrated with the \"main\" language thus making the task easier. For those who don\'t know CL-WHO, it looks

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 05:20

    Clojure

    There are a bunch of CL-WHO-inspired HTML-generating libraries available in Clojure (as one would expect, Clojure being a Lisp). Here's how you could do it using the HTML library that comes with Compojure, and cl-format:

    (use 'compojure.html
         'com.infolace.format)
    
    (html
     [:table {:border 0 :cellpadding 4}
      (map (fn [tds] [:tr {:align "right"} tds])
           (partition 5 (map (fn [num color]
                               [:td {:bgcolor color}
                                (cl-format nil "~@R" (inc num))])
                             (range 25)
                             (cycle ["green" "pink"]))))])
    

    Compojure's HTML library makes good use of Clojure's literal hash-maps as attribute/value pairs, and using literal vectors for tags instead of lists for everything helps the tags stand out a bit and avoids some of the need for macro magic.

    partition breaks up a collection into groups of some number of elements. cycle generates an infinitely repeating list of the elements of a collection. These plus range and map help you avoid explicit loops and counter variables.

提交回复
热议问题