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
Post a Comment