Trouble with building up a string in Clojure

Posted by Aki Iskandar on Stack Overflow See other posts from Stack Overflow or by Aki Iskandar
Published on 2011-01-08T23:35:08Z Indexed on 2011/01/09 0:54 UTC
Read the original article Hit count: 258

Filed under:

Hi gang - [this may seem like my problem is with Compojure, but it isn't - it's with Clojure]

I've been pulling my hair out on this seemingly simple issue - but am getting nowhere.

I am playing with Compojure (a light web framework for Clojure) and I would just like to generate a web page showing showing my list of todos that are in a PostgreSQL database.

The code snippets are below (left out the database connection, query, etc - but that part isn't needed because specific issue is that the resulting HTML shows nothing between the <body> and </body> tags).

As a test, I tried hard-coding the string in the call to main-layout, like this: (html (main-layout "Aki's Todos" "Haircut<br>Study Clojure<br>Answer a question on Stackoverfolw")) - and it works fine.

So the real issue is that I do not believe I know how to build up a string in Clojure. Not the idiomatic way, and not by calling out to Java's StringBuilder either - as I have attempted to do in the code below.

A virtual beer, and a big upvote to whoever can solve it! Many thanks!

=============================================================


;The master template (a very simple POC for now, but can expand on it later)

(defn main-layout
  "This is one of the html layouts for the pages assets - just like a master page"
  [title body]
  (html
    [:html
    [:head
      [:title title]
      (include-js "todos.js")
      (include-css "todos.css")]
    [:body body]]))


(defn show-all-todos 
  "This function will generate the todos HTML table and call the layout function"
  []
  (let [rs (select-all-todos)
       sbHTML (new StringBuilder)]
    (for [rec rs]
      (.append sbHTML (str rec "<br><br>"))) 
    (html (main-layout "Aki's Todos" (.toString sbHTML)))))

=============================================================

Again, the result is a web page but with nothing between the body tags. If I replace the code in the for loop with println statements, and direct the code to the repl - forgetting about the web page stuff (ie. the call to main-layout), the resultset gets printed - BUT - the issue is with building up the string.

Thanks again.

~Aki

© Stack Overflow or respective owner

Related posts about clojure