Posted on 22-01-2014
Filed Under (Technology) by bigbang

I am using Clojure & Compojure to write my webpages, and I am trying to use Selmer as template system for Clojure.

Today I find a strange thing in my template file. I used following code to take the position of real content:

{{content}}

Then I generate real html content in my clojure code like this:

(defn index-render [params]
    "render file content of index.html"
    (let [basecon "<B>to be done</B>"]
    (render-file "./myproject/templates/front.tpl" 
               {:content basecon})))

But after render the content, the page just shows following content:

<B>to be done</B>

and I printed the html result I found following content in html:

&lt;B&gt;to be done&lt;/B&gt;

which means the render has escaped < and > to &lt; and &gt; for me.

The solution is very easy. Just use filter in template files.

Selmer is basically the same as The Django template language, so the Automatic HTML escaping is also basically the same. According to Filters part in Selmer online doc, the problem can be done by specifying a filter ‘safe’ following the name of the variable. The filters are separated using the | character.

So I change my templete file into

{{content|safe}}

and it works fine!

© 2014, The Big Bang on Reading Busy. All rights reserved.
On republishing this post you must provide link to the original post.

Post a Comment
Name:
Email:
Website:
Comments: