user6324692
user6324692

Reputation:

How can I preprocess data before displaying it in Luminus/Compojure?

I have this in Luminus/Compojure project:

(defn article-show-single [id]
  (let [a (db/get-single-article {:id id})]
  (layout/render "show.html"
    {:article a}))

Now I want to preprocess the the :body of an article. I can this by:

(str/replace (:body a) #"regex123"
           (fn [[_ var1 var2]]
               (str "new str 123")))
; => new str 123

But how can I combine those 2, that is, I want to change the :body of an article and still return the article. How can I do this?

Upvotes: 0

Views: 55

Answers (1)

Piotrek Bzdyl
Piotrek Bzdyl

Reputation: 13175

As the first step I would extract your preprocessing code as a function to make the code more readable:

(defn preprocess [s]
  (str/replace s
               #"regex123"
               (fn [[_ var1 var2]]
                 (str "new str 123"))))

Then I would use update function to update the value of a map's key by applying a provided function to the current value and use that value in the new version of the map:

(update article :body preprocess)

Upvotes: 0

Related Questions