UTF-8 characters mangled

103 views
Skip to first unread message

gert

unread,
Nov 20, 2011, 8:06:51 PM11/20/11
to Enlive
Hi all,

I'm learning to use Enlive in combination with Noir. I have an html
template, saved as UTF-8, with non-ascii characters (such as ©). When
I serve this file directly from Apache, it works fine. However, when I
read the file through net.cgrand.enlive-html/template and serve it
with jetty, UTF-8 characters are mangled.

I'm starting a Jetty server with:

(noir.server/start 8080)

and I serve the file on url /page like this:

(defpage "/page" []
(html/template "web/views/page.html" []))

Firefox says (in its view->character encoding menu) the file is UTF-8
encoded. I tried using html entities (&copy instead of ©) without
luck.

What am I missing?

With kind regards,
Gert

Steve Miner

unread,
Nov 21, 2011, 5:09:02 PM11/21/11
to enliv...@googlegroups.com
I'm not sure about your specific problem, but I resolved something like this by fixing my templates.

Take a look at your HTML templates and make sure that they declare the correct encoding (utf-8 is a good choice). Here's what I use at the top of my files. Notice the charset.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


I hope that helps.

Steve

Gert Verhoog

unread,
Nov 21, 2011, 6:02:27 PM11/21/11
to enliv...@googlegroups.com
Thanks Steve, I've tried that - both the http-equiv line you mentioned and the html5 <meta charset="utf-8"> - to no avail.

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

Huahai Yang

unread,
Dec 24, 2011, 1:24:36 AM12/24/11
to Enlive
I encountered similar problem a few months ago. This problem does not
concern enlive though. It is a problem with compojure (noir uses
compojure behind the scene), which does not set charset by default. It
can be solved by adding this ring middleware:

; needed to fix a charset problem in compojure
(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))))

(def app (handler/site
; call wrap-charset fn, set default charset
(wrap! main-routes (:charset "utf-8"))))

If this code doesn't work for you, you may ask in the compojure or
noir google groups.

-huahai
Reply all
Reply to author
Forward
0 new messages