Problem with Compojure 0.4 + Enlive and UTF-8

246 views
Skip to first unread message

Nebojša Stričević

unread,
Jun 9, 2010, 1:59:38 PM6/9/10
to Compojure
Greetings,

I'm fairly new to Clojure, and completely new to Compojure and Enlive.
I'm building a small web application using these tools and everything
is going great, except using Serbian characters in Enlive templates.
Now, when i started I was using Compojure 0.3.2 and I had no problems
with Serbian characters. Problem happened after migration to 0.4, so
I'm assuming it could be related with Compojure. If I'm wrong, please
let me know where to look if you have a clue. Here is the code
(index.html is just simple html file with some Serbian characters and
it has <meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/> in head):

Compojure 0.4

(ns master-temp.hello
(:require [net.cgrand.enlive-html :as enlive])
(:use compojure.core)
(:use ring.adapter.jetty))

(enlive/deftemplate index "master_temp/templates/index.html"
[])

(defroutes main-routes
(GET "*" [] (apply str (index))))

(run-jetty main-routes {:join? false :port 8080})

Compojure 0.3.2

(ns master-temp.hello
(:require [net.cgrand.enlive-html :as enlive])
(:use compojure))

(enlive/deftemplate index "master_temp/templates/index.html"
[])

(defroutes main-routes
(GET "*" [] (apply str (index))))

(run-server {:port 8080}
"/*" (servlet main-routes))

Thanks in advance.

James Reeves

unread,
Jun 9, 2010, 2:49:27 PM6/9/10
to Compojure
On Jun 9, 6:59 pm, Nebojša Stričević <nebojsa.strice...@gmail.com>
wrote:
> I'm fairly new to Clojure, and completely new to Compojure and Enlive.
> I'm building a small web application using these tools and everything
> is going great, except using Serbian characters in Enlive templates.

Could you provide an example index file and a description of how
exactly the Serbian characters are affected?

This may be a problem with the character encoding in the Ring Jetty
adapter, but I'll need to investigate further to be certain.

- James

Nebojsa Stricevic

unread,
Jun 9, 2010, 3:02:35 PM6/9/10
to comp...@googlegroups.com
Sure. This is html file example:

<html>
    <head>
        <title id="title">Naslov</title>
        <link type="text/css" rel="stylesheet" href="style.css"/>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    </head>
    <body>
        <div id="headline">
        </div>

        <div id="main">
            <div id="left">
                <h3>Meni</h3>
            </div>

            <div id="right">
                <h1 id="title">Naslov</h1>
                <p id="message">Poruka za korisnika<p>

                <div id="content">
                    <p>šđčć</p>
                </div>
            </div>
        </div>
    </body>
</html>

Notice <p>šđčć</p>. Well, these characters are displayed as ? (question mark).


--
You received this message because you are subscribed to the Google Groups "Compojure" group.
To post to this group, send email to comp...@googlegroups.com.
To unsubscribe from this group, send email to compojure+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/compojure?hl=en.




--
Nebojša Stričević

mpenet

unread,
Jun 9, 2010, 5:08:46 PM6/9/10
to Compojure
Hi,

Does adding -Dfile.encoding=UTF-8 on the java command you use to run
your app makes any difference?

On Jun 9, 7:59 pm, Nebojša Stričević <nebojsa.strice...@gmail.com>
wrote:

James Reeves

unread,
Jun 9, 2010, 5:45:23 PM6/9/10
to comp...@googlegroups.com
On 9 June 2010 20:02, Nebojsa Stricevic <nebojsa....@gmail.com> wrote:
> Notice <p>šđčć</p>. Well, these characters are displayed as ? (question
> mark).

Okay, this looks like an issue with the content-type charset. Here's a
temporary solution:

(defn wrap-charset [handler charset]
(fn [request]
(if-let [response (handler request)]
(if-let [content-type (get-in response [:headers "Content-Type"])]
(if (.contains content-type "charset")
response
(assoc-in response
[:headers "Content-Type"]
(str content-type "; charset=" charset)))
response))))

This middleware function will force the charset to be whatever you choose:

(wrap! main-routes (:charset "utf8"))

I'm going to investigate this a little further and try to come up with
a better solution.

- James

Nebojsa Stricevic

unread,
Jun 9, 2010, 6:37:06 PM6/9/10
to comp...@googlegroups.com
@mpenet: I've tried to put ":properties { :project.build.sourceEncoding "UTF-8" }" in Leiningen project.clj. Is that equivalent? (I'm new to Leiningen also and I have no experience with Maven.)

@James: Thanks a lot for a quick fix! I really appreciate it! Looking forward to continue my journey...

Greets,


- James

--
You received this message because you are subscribed to the Google Groups "Compojure" group.
To post to this group, send email to comp...@googlegroups.com.
To unsubscribe from this group, send email to compojure+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/compojure?hl=en.



--
Nebojša Stričević
Reply all
Reply to author
Forward
0 new messages