Clojure: partly change an attribute value in Enlive -


i have test.html file contains:

<div class="clj-test class1 class2 col-sm-4 class3">content</div> 

a want define template changes part of html attr value:

(deftemplate test "public/templates/test.html" []   [:.clj-test] (enlive/set-attr :class (partly-change-attr #"col*" "col-sm-8"))) 

this render:

... <div class="clj-test class1 class2 col-sm-8 class3">content</div> ... 

thanks help!

just found update-attr fn suggested christophe grand in thread:

(defn update-attr [attr f & args]   (fn [node] (apply update-in node [:attrs attr] f args))) 

pretty cool! can use directly :

(enlive/deftemplate test-template "templates/test.html" []   [:.clj-test] (update-attr :class clojure.string/replace #"col-.*?(?=\s)" "col-sm-8")) 

or build more specific fn:

(defn replace-attr [attr pattern s]       (update-attr attr clojure.string/replace pattern s))  (enlive/deftemplate test-template "templates/test.html" []       [:.clj-test] (replace-attr :class #"col-.*?(?=\s)" "col-sm-8")) 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -