97 views
Skip to first unread message

Cecil Westerhof

unread,
Feb 26, 2015, 2:40:05 PM2/26/15
to clo...@googlegroups.com
At the moment I have the following code:
    (str "<html>"
         "<table border='1' cellpadding='10'"
         "style='font-family:Arial; font-size:16px; margin: 10px;width:100%'>"
         "<tr>"
         "<th bgcolor='black' color='white'>Quote</th>"
         "<th bgcolor='black' color='white'>Author</th>"
         "</tr>")

But I want to make it more generic: the th lines should be data-driven. There is a headers variable and when this contains:
    '(:quote "Quote" :author "Author")

Then the above should be generated. So for every odd position (counting from 0) a th string should be inserted. How would I do this?

--
Cecil Westerhof

Timothy Baldridge

unread,
Feb 26, 2015, 2:45:46 PM2/26/15
to clo...@googlegroups.com
I normally either do something like this:

(apply str (concat ["a" "b"]
                            (map name :c :d)
                            ["e" "f"]))

Or use java's StringBuilder, it's a mutable black box, but appending is pretty efficient, and doesn't involve the creation of a ton of seqs like the above method does. 

Timothy

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
“One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.”
(Robert Firth)

Colin Yates

unread,
Feb 26, 2015, 2:47:22 PM2/26/15
to clo...@googlegroups.com
Hi Cecil - have you looked at hiccup?

Cecil Westerhof

unread,
Feb 26, 2015, 3:23:17 PM2/26/15
to clo...@googlegroups.com
2015-02-26 20:45 GMT+01:00 Timothy Baldridge <tbald...@gmail.com>:
I normally either do something like this:

(apply str (concat ["a" "b"]
                            (map name :c :d)
                            ["e" "f"]))
 
​That gives:
    IllegalArgumentException Don't know how to create ISeq from: clojure.lang.Keyword  clojure.lang.RT.seqFrom (RT.java:505)

But I found something that works. I'll post it in another reply.​


Or use java's StringBuilder, it's a mutable black box, but appending is pretty efficient, and doesn't involve the creation of a ton of seqs like the above method does.

​That is still done. That is not good?

 
On Thu, Feb 26, 2015 at 12:39 PM, Cecil Westerhof <cldwes...@gmail.com> wrote:
At the moment I have the following code:
    (str "<html>"
         "<table border='1' cellpadding='10'"
         "style='font-family:Arial; font-size:16px; margin: 10px;width:100%'>"
         "<tr>"
         "<th bgcolor='black' color='white'>Quote</th>"
         "<th bgcolor='black' color='white'>Author</th>"
         "</tr>")

But I want to make it more generic: the th lines should be data-driven. There is a headers variable and when this contains:
    '(:quote "Quote" :author "Author")

Then the above should be generated. So for every odd position (counting from 0) a th string should be inserted. How would I do this?

--
Cecil Westerhof

Cecil Westerhof

unread,
Feb 26, 2015, 3:23:59 PM2/26/15
to clo...@googlegroups.com
2015-02-26 20:46 GMT+01:00 Colin Yates <colin...@gmail.com>:
Hi Cecil - have you looked at hiccup?
 

​Not yet.​

On 26 February 2015 at 19:39, Cecil Westerhof <cldwes...@gmail.com> wrote:
> At the moment I have the following code:
>     (str "<html>"
>          "<table border='1' cellpadding='10'"
>          "style='font-family:Arial; font-size:16px; margin:
> 10px;width:100%'>"
>          "<tr>"
>          "<th bgcolor='black' color='white'>Quote</th>"
>          "<th bgcolor='black' color='white'>Author</th>"
>          "</tr>")
>
> But I want to make it more generic: the th lines should be data-driven.
> There is a headers variable and when this contains:
>     '(:quote "Quote" :author "Author")
>
> Then the above should be generated. So for every odd position (counting from
> 0) a th string should be inserted. How would I do this?
​​
 
--
Cecil Westerhof

Cecil Westerhof

unread,
Feb 26, 2015, 3:30:11 PM2/26/15
to clo...@googlegroups.com
​I found a solution. Do not know how good it is Clojure wise, but it certainly does what I want:

    (str "<html>"
         "<table border='1' cellpadding='10'"
         "style='font-family:Arial; font-size:16px; margin: 10px;width:100%'>"
         "<tr>"
         (loop [headings     (rest headers)
               html-headings ""]
               (let [header (first headings)]
                    (if (empty? headings)
                        html-headings
                      (recur (rest (rest headings))
                             (str html-headings
                                  (format "<th bgcolor='black' color='white'>%s</th>" header))))))
         "</tr>")

--
Cecil Westerhof

Colin Yates

unread,
Feb 26, 2015, 4:46:22 PM2/26/15
to clo...@googlegroups.com
I mention it because it is a very elegant solution to the problem of constructing html in clojure. An equivalent of your code would be something like:

(html
 (html5
   [:table {:border: 1 :cellpadding :10 :style {:font-family "Arial" :font-size "16px" :margin "10px" :width "100%"}}
     [:tr
       [:th {:bgcolor "black" :color "white"} "Quote"]
       [:th {:bgcolor "black" :color "white"} "Author"]]]))

The great thing is that it is just data structures so you can do things like:

(def headers ["Quote" "Author"])

(html
 (html5
   [:table {:border: 1 :cellpadding :10 :style {:font-family "Arial" :font-size "16px" :margin "10px" :width "100%"}}
     [:tr
       (for [header headers]
         [:th {:bgcolor "black" :color "white"} "header"])]]))

HTH.
Reply all
Reply to author
Forward
0 new messages