Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with building up a string in Clojure
    text
    copied!<p>[this may seem like my problem is with Compojure, but it isn't - it's with Clojure]</p> <p>I've been pulling my hair out on this seemingly simple issue - but am getting nowhere.</p> <p>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. </p> <p>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 &lt;body&gt; and &lt;/body&gt; tags).</p> <p>As a test, I tried hard-coding the string in the call to main-layout, like this: (html (main-layout "Aki's Todos" "Haircut&lt;br&gt;Study Clojure&lt;br&gt;Answer a question on Stackoverfolw")) - and it works fine.</p> <p>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.</p> <p>A virtual beer, and a big upvote to whoever can solve it! Many thanks!</p> <p>=============================================================</p> <pre><code> ;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 "&lt;br&gt;&lt;br&gt;"))) (html (main-layout "Aki's Todos" (.toString sbHTML))))) </code></pre> <p>=============================================================</p> <p>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.</p> <p>Thanks again.</p> <p>~Aki</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload