how to get this working in webnoir

70 Views Asked by At

I am trying to do this in webnoir.

This works:

(defpage [:post "/testurl] {:keys [name phone]}
  (html5
    (str "name: " name)
    (str "phone: " phone)))

Now I want to generate defpages for many modules, each has a list of different fields. And I want to call the defpages from a function. The defpage must accept post for the fields.

Basically I have this: (def fields1 ["Name" "Phone" "Email" "xyz"])

And I would like to pass this to defpage, instead of having to specify the keys manually.

The fields might change in the future and that's why I want my code to pick up the fields and create the defpages dynamically on server startup.

Is it possible?

Thank you for all your help!

1

There are 1 best solutions below

1
Ankur On

You can do this with a macro:

(defmacro defpages [pages]
  `(do
     ~@(map (fn [page]
              `(~'defpage [:post ~(str "/" (page :name))]
                 {:keys ~(into [] (map symbol (page :fields)))}
                 (~'html5
                  ~@(map (fn [field]
                           `(str ~(str field ": ")
                                 ~(symbol field)))
                         (page :fields))))) pages)))



(defpages [{:name "testurl"
            :fields ["name" "phone"]}
           {:name "user"
            :fields ["age" "address"]}])