1

I'm rewriting my History of the World website (history.clmitchell.net) in clojure and making great progress. I've reached the point where I need to use a conditional to output hiccup code for declaring CSS links based on which page I'm loading. I've pored over the docs but can't figure this out:

(defn cssfiles
  "load sets of CSS files depending on which page is being served"
  [pageName]
  (condp = pageName
         "index" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/wc3xhtml1.css")
         "add" (hic/include-css "/css/hcspry.css"
                                "/css/menus.css"
                                "/css/addform.css")
         "map" (hic/include-css "/css/hcspry.css"
                                "/css/menus.css"
                                "/css/SpryCollapsiblePanel.css"
                                "/css/SpryAccordion.css")
         "chart" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/cha.css")
         "learn" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/wc3xhtml1.css"
                                  "/css/doc.css")
         "changes" (hic/include-css "/css/hcspry.css"
                                    "/css/menus.css"
                                    "/css/filter.css"
                                    "/css/wc3xhtml1.css"
                                    "/css/clg.css")
         "seek" (hic/include-css "/css/hcspry.css"
                                 "/css/menus.css"))
  (println (str  "PRGMR. MSG: net.clm.history.pages.snippets.clj::(defn cssfiles [" pageName "]): unknown parameter or parameter not processed properly.")))

Question: what is the proper syntax for this kind of function?

Quasaur
  • 1,335
  • 1
  • 10
  • 27
  • Can you clarify the question? If you're trying to have `println` as the `:else` condition, then you need to "slurp" it inside the previous set of parentheses (ones belonging to `condp`). – dskrvk Aug 14 '15 at 06:17
  • @dskrvk I think you're right; I'll try that 1st thing tomorrow. – Quasaur Aug 14 '15 at 07:22
  • It isn't clear fro your question what it is you cannot figure out. What is your function not doing which you expect it should be doing? – Tim X Aug 14 '15 at 09:51
  • This isn't a question. It's a statement of some things you are doing, and then a paste of your code. What are you asking? – amalloy Aug 14 '15 at 18:57

3 Answers3

2

You could define your page-css mapping as a separate, plain data structure and avoid the repetitive call to hic/include-css:

(def page-css-mapping
  {"index"   ["/css/hcspry.css"
              "/css/menus.css"
              "/css/filter.css"
              "/css/wc3xhtml1.css"]
   "add"     ["/css/hcspry.css"
              "/css/menus.css"
              "/css/addform.css"]
   "map"     ["/css/hcspry.css"
              "/css/menus.css"
              "/css/SpryCollapsiblePanel.css"
              "/css/SpryAccordion.css"]
   "chart"   ["/css/hcspry.css"
              "/css/menus.css"
              "/css/filter.css"
              "/css/cha.css"]
   "learn"   ["/css/hcspry.css"
              "/css/menus.css"
              "/css/filter.css"
              "/css/wc3xhtml1.css"
              "/css/doc.css"]
   "changes" ["/css/hcspry.css"
              "/css/menus.css"
              "/css/filter.css"
              "/css/wc3xhtml1.css"
              "/css/clg.css"]
   "seek"    ["/css/hcspry.css"
              "/css/menus.css"]})

(defn cssfiles
  "load sets of CSS files depending on which page is being served"
  [pageName]
  (if-let [css-files (get page-css-mapping pageName)]
    (apply hic/include-css css-files)
    (println (str "PRGMR. MSG: net.clm.history.pages.snippets.clj::(defn cssfiles [" pageName "]): unknown parameter or parameter not processed properly."))))

Now you can also store your mapping as plain data structure (eg. as EDN file or in Datomic) instead of having it hard coded.

Jochen Rau
  • 390
  • 1
  • 6
1

Are you saying that you're not sure what the function cssfiles is doing? In that case, the function says:

If my argument pagename is equal to the string "index", then execute the function include-css that can be found in the namespace abbreviated by hic, passing this function the arguments "/css/hcspry.css", "/css/menus.css", ..., and returning the resulting value as my (i.e. cssfiles') return value.

If instead pagename is equal to "add", then excecute include-css with a different set of arguments ..., returning the resulting value .... (etc.)

And so on for other strings that are predictable values of pagename.

But if pagename doesn't match any of the strings I tested, then execute the println statement at the end of the condp expression in order to print out an error message.

See the documentation for condp for more on the syntax. I'm happy to provide more details if needed, though.

Mars
  • 8,689
  • 2
  • 42
  • 70
  • @dskrv is correct about the misplaced parentheses. Everything I said applies with the correction. – Mars Aug 14 '15 at 15:17
0

dskrvk was right: the println form was not within the condp form; here's the corrected frunction:

(defn cssfiles
  "load sets of CSS files depending on which page is being served"
  [pageName]
  (condp = pageName
         "index" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/wc3xhtml1.css")
         "add" (hic/include-css "/css/hcspry.css"
                                "/css/menus.css"
                                "/css/addform.css")
         "map" (hic/include-css "/css/hcspry.css"
                                "/css/menus.css"
                                "/css/SpryCollapsiblePanel.css"
                                "/css/SpryAccordion.css")
         "chart" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/cha.css")
         "learn" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/wc3xhtml1.css"
                                  "/css/doc.css")
         "changes" (hic/include-css "/css/hcspry.css"
                                    "/css/menus.css"
                                    "/css/filter.css"
                                    "/css/wc3xhtml1.css"
                                    "/css/clg.css")
         "seek" (hic/include-css "/css/hcspry.css"
                                 "/css/menus.css")
         (println (str  "PRGMR. MSG: net.clm.history.pages.snippets.clj::(defn cssfiles [" pageName "]): unknown parameter or parameter not processed properly."))))
Quasaur
  • 1,335
  • 1
  • 10
  • 27
  • It's not clear why this is an answer @Quasaur. I was never sure what the original question was. – Mars Aug 14 '15 at 15:18