Blogs of Jiawei Zhao

CloServe, View First Web Framework in Clojure

View first web framework as Lift separates web design and backend code nicely. With view first framework, one do not need to interleve HTML with non HTML code to generate dynamic web pages, sometimes geneate half tags here and there :). Instead, one can use design tools to design pure HTML pages, and just mark the HTML elements which need to change dynamically in a HTML standard compilance way. After such marking, the HTML file stay as HTML and can be viewed and edited with all HTML editors still. The web framework will parse the HTML file, check for the special marks, and change the element accordingly. To see how these works for eg in Lift, check out the “Simply Lift”, “Seven Things about Lift”.

Clojure has some fantistic web frameworks based on Ring,  and packages like hickory, enlive, laser, hiccup et al to deal with HTML. One can already use the existing web frameworks for Clojure in a view first way, making use of enlive, hickory  or laser to transform the HTML document. Just so far as I know, using this method one need to somehow deal with the HTML file as a whole, with a quite strong coupling between HTML file and backend code still.

So I started a project CloServe  to bring Lift like easy to use view first web frameworks to Clojure. With CloServe, the “Contact Us” page on this website is driven now by such code snippet:

   <form class="lift:form.ajax?callback=UserFeedBack">

The correspond callback code is quite simple:

 [context cmd]
 (db/insert! :feedbacks (assoc cmd :create-time (java.util.Date.)))
 (send-cmd-to-page (:page-id context) {:act :hide :id (str "#" (:form-id context))})
 (send-cmd-to-page (:page-id context) 
                   {:act :replaceWith :selector (str "#contactinfo")
                    :html "<p><em>Thanks for submitting ...</em></p>"})

This code snippet will save user feedback into database, tell the browser to hide the contact form, and write a success message on the same page. 

There are more examples at github closerve-example

comments powered by Disqus

Convert Sandvox form to work with Lift

While I like Sandvox a lot and used it to make a few pages for my company, I want to make a script to convert the contact form generated by sandbox to be processed by the lift snippet mechanism. That is quite easy to do with the Clojure laser module:

File core.clj:

(ns convert-sandvox-form.core
  (:require [me.raynes.laser :as l]
            [ :refer [file]])

(defn -main [filename]

  (let [ corrhtml (l/document (l/parse (file filename))
                              (l/descendant-of (l/id= "contactform")
                                               (l/element= :script)) (l/remove)
                              (l/descendant-of (l/element= :form)
                                               (l/class= "hidden")) (l/remove)
                              (l/element= :form) (l/attr :action "")
                              (l/element= :form) (l/attr :onsubmit "")
                              (l/element= :form) (l/attr :method "")
                              (l/element= :form) (l/add-class "lift:UserFeedBack?form=post")
                              (l/id= "contactform-results") (l/add-class "lift:Msg?id=msgcontact&errorClass=error")
    (spit filename corrhtml)))

It will overwrite the original HTML file.

comments powered by Disqus