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!

(0) Comments    Read More   
Posted on 16-01-2014
Filed Under (Technology) by bigbang

There are many basic startup code examples for Compojure on the web, but I don’t see any kind of deployment tutorial. Here are some basic collections if you want to use Clojure to start a web development.

  1. First, clone the code of compojure-example, and run it with the instructions on the page. If you can run it successfully by command
    lein ring server
    

    , then you can proceed to next step.

  2. Try to develop your web site with the lein ring server. This is the best way to develop Clojure web application for it can refresh your code automatically.
  3. Here comes the final step to deploy Clojure code to web server to provide official service for other people. First, package all resources such as code, css, html template files to war file:
    lein ring uberwar
    

    Then config your Tomcat according to manual and deploy the war file with http://localhost:8080/manager. There is a “WAR file to deploy” option on the manager page. It is the key point of deploying Clojure service to Tomcat. You can just think it is a Java application and use the normal way to deploy it.

BTW: the Tomcat root folder is something like D:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT. If you web application can’t load some resource file, please check the relative path for it.

(0) Comments    Read More