Posted on 23-02-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.

Here is my template file structure: there is a basic template file base.tpl which defines basic header, footer or something else. And some other files like index.tpl, search.tpl to do the real job by including base.tpl template. The base.tpl file defines the common part of my site and changes few times during my project lifetime.

In some suitations, we need to modify base.tpl. But after modification of base.tpl, Clojure & Selmer don’t re-render base.tpl when I visit and render index.tpl file. So I need to restart lein ring server every time after I modify base.tpl.

It is too troublesome and forgettable to restart lein ring server every time after modified base.tpl. Finally I find the reason on Selmer official doc site:

Note that changes in files referenced by the template will not trigger a recompile. This means that if your template extends or includes other templates you must touch the file that’s being rendered for changes to take effect.

Alternatively you can turn caching on and off using

(selmer.parser/cache-on!)

and

(selmer.parser/cache-off!)

respectively.

(0) Comments    Read More   
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