<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>VijayKiran.com</title>
	
	<link>http://www.vijaykiran.com</link>
	<description />
	<lastBuildDate>Tue, 08 May 2012 16:02:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Vijaykirancom" /><feedburner:info uri="vijaykirancom" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>Vijaykirancom</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>→ Clojure/core — Reducers – A Library and Model for Collection Processing</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/PNTMEfxANNg/</link>
		<comments>http://www.vijaykiran.com/2012/05/08/clojurecore-reducers-a-library-and-model-for-collection-processing/#comments</comments>
		<pubDate>Tue, 08 May 2012 16:00:59 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Links]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3850</guid>
		<description><![CDATA[We now have a new super-generalized and minimal abstraction for collections &#8211; a collection is some set of things that, when given a function to apply to its contents, can do so and give you the result, i.e. a collection is (at minimum) reducible. In other words, you can call reduce on it. via Clojure/core [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>We now have a new super-generalized and minimal abstraction for collections &#8211; a collection is some set of things that, when given a function to apply to its contents, can do so and give you the result, i.e. a collection is (at minimum) reducible. In other words, you can call reduce on it.
</p></blockquote>
<p>via <a href='http://clojure.com/blog/2012/05/08/reducers-a-library-and-model-for-collection-processing.html'>Clojure/core — Reducers &#8211; A Library and Model for Collection Processing</a>.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/WuRc98sD5wO9l0B0VqBAPdbILjY/0/da"><img src="http://feedads.g.doubleclick.net/~a/WuRc98sD5wO9l0B0VqBAPdbILjY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/WuRc98sD5wO9l0B0VqBAPdbILjY/1/da"><img src="http://feedads.g.doubleclick.net/~a/WuRc98sD5wO9l0B0VqBAPdbILjY/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=PNTMEfxANNg:C5VtkQWmjek:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=PNTMEfxANNg:C5VtkQWmjek:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=PNTMEfxANNg:C5VtkQWmjek:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/PNTMEfxANNg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/05/08/clojurecore-reducers-a-library-and-model-for-collection-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/05/08/clojurecore-reducers-a-library-and-model-for-collection-processing/</feedburner:origLink></item>
		<item>
		<title>★ Castiglione del Lago</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/WL68kkhDyv8/</link>
		<comments>http://www.vijaykiran.com/2012/04/18/castiglione-del-lago/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 17:58:14 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Photography]]></category>
		<category><![CDATA[italy]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3846</guid>
		<description />
			<content:encoded><![CDATA[<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/04/2012-04-10-at-14-06-43.jpg" class="enlarge"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/04/2012-04-10-at-14-06-43-620x413.jpg" alt="" title="2012-04-10 at 14-06-43" width="620" height="413" class="aligncenter size-large wp-image-3847" /></a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/nJJc6dGOQ7tiRGgKYCb_RSb3AyI/0/da"><img src="http://feedads.g.doubleclick.net/~a/nJJc6dGOQ7tiRGgKYCb_RSb3AyI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/nJJc6dGOQ7tiRGgKYCb_RSb3AyI/1/da"><img src="http://feedads.g.doubleclick.net/~a/nJJc6dGOQ7tiRGgKYCb_RSb3AyI/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=WL68kkhDyv8:AV8CVxQJ1bA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=WL68kkhDyv8:AV8CVxQJ1bA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=WL68kkhDyv8:AV8CVxQJ1bA:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/WL68kkhDyv8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/04/18/castiglione-del-lago/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/04/18/castiglione-del-lago/</feedburner:origLink></item>
		<item>
		<title>★ Italia</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/J5If0Vcf_Dg/</link>
		<comments>http://www.vijaykiran.com/2012/04/18/italia/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 23:14:30 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Photography]]></category>
		<category><![CDATA[florence]]></category>
		<category><![CDATA[italy]]></category>
		<category><![CDATA[photos]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3841</guid>
		<description />
			<content:encoded><![CDATA[<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/04/2012-04-15-at-13-03-31.jpg" class="enlarge"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/04/2012-04-15-at-13-03-31-620x413.jpg" alt="" title="2012-04-15 at 13-03-31" width="620" height="413" class="aligncenter size-large wp-image-3842" /></a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/gfX3llMBLzN2qpSPNjR94tO5KT8/0/da"><img src="http://feedads.g.doubleclick.net/~a/gfX3llMBLzN2qpSPNjR94tO5KT8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/gfX3llMBLzN2qpSPNjR94tO5KT8/1/da"><img src="http://feedads.g.doubleclick.net/~a/gfX3llMBLzN2qpSPNjR94tO5KT8/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=J5If0Vcf_Dg:cXFQoXaVJI8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=J5If0Vcf_Dg:cXFQoXaVJI8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=J5If0Vcf_Dg:cXFQoXaVJI8:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/J5If0Vcf_Dg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/04/18/italia/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/04/18/italia/</feedburner:origLink></item>
		<item>
		<title>★ Rome – Saint Peter’s Square</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/kPY_uTJYvEE/</link>
		<comments>http://www.vijaykiran.com/2012/04/13/%e2%98%85-rome-saint-peters-square/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 08:46:27 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Photography]]></category>
		<category><![CDATA[rome]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3836</guid>
		<description />
			<content:encoded><![CDATA[<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/04/st-peter-square.jpg"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/04/st-peter-square-620x413.jpg" alt="" title="st-peter-square" width="620" height="413" class="aligncenter size-large wp-image-3837" /></a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/y-t16E9KCdun-XVaBVJnFSbqVmc/0/da"><img src="http://feedads.g.doubleclick.net/~a/y-t16E9KCdun-XVaBVJnFSbqVmc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/y-t16E9KCdun-XVaBVJnFSbqVmc/1/da"><img src="http://feedads.g.doubleclick.net/~a/y-t16E9KCdun-XVaBVJnFSbqVmc/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=kPY_uTJYvEE:kLNWhtAqXoE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=kPY_uTJYvEE:kLNWhtAqXoE:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=kPY_uTJYvEE:kLNWhtAqXoE:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/kPY_uTJYvEE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/04/13/%e2%98%85-rome-saint-peters-square/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/04/13/%e2%98%85-rome-saint-peters-square/</feedburner:origLink></item>
		<item>
		<title>Web Application Development with Clojure – Part 5</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/zjk_79dAPYA/</link>
		<comments>http://www.vijaykiran.com/2012/02/27/web-application-development-with-clojure-part-5/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 00:44:11 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[webapplication]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3799</guid>
		<description><![CDATA[In this part of the Clojure Web application development blog post series, we'll see how to create a simple secured administration area using ring session middleware. We'll wrap up the blog engine by adding a simple form that will submit data to the server and add code to create a new blog post. This part completes all the necessary parts to create a simple blog engine. In the next and final paart of this series we'll cover the deployment options available for deploying this application to production.]]></description>
			<content:encoded><![CDATA[<p>This post is part of the Web Application Development with <a href="http://clojure.org">Clojure</a> tutorial. You might want to read the previous posts before this post for continuity&#8217;s sake.</p>
<ul>
<li><a href="http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/">Part 1: Project Setup</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/">Part 2: Data model definition with Lobos and Korma</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/31/web-application-development-with-clojure-part-3/">Part 3: Loading Fixures with <a href="https://github.com/lancepantz/clj-yaml">clj-yaml</a> and HTML Templating with <a href="https://github.com/cgrand/enlive">Enlive</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/12/web-application-development-with-clojure-part-4/">Part 4: Adding CSS, Post detail page and Simple Authentication</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/27/web-application-development-with-clojure-part-5/">Part 5: Blog Administration Area</a></li>
</ul>
<h2>Introduction</h2>
<p>Over that past 4 parts you have seen how to start with creating a simple blog using Clojure, render the posts and finally how to add a simple login page that will check the username and password and redirect to &#8220;admin&#8221; page. In this part we&#8217;ll finish the administration area of the blog that will help us create new blog posts.  </p>
<h3>Source code on github</h3>
<p>The code for this series is now available on <a href="https://github.com/vijaykiran/clog">github</a> and the source code is tagged with part names. If you want to checkout the code for a specific part of this tutorial you can do so using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">git clone git://github.com/vijaykiran/clog.git
cd clog
git checkout part5</pre></div></div>

<h2>Sessions</h2>
<div class="asideBlock">You can read more about ring&#8217;s session and cookie store <a href="https://github.com/mmcgrana/ring/wiki/Sessions">here</a> and <a href="https://github.com/mmcgrana/ring/wiki/Cookies">here</a></div>
<p>When a user logs into the &#8220;admin&#8221; area using our login form, we need to keep track of her session and allow them to perform administrative operations such as managing posts or setting their password etc. Ring library provides a simple session middleware that uses various session storage options &#8211; cookie, in-memory and database backed session stores. You can create your own session implementations by implementing the SessionStore protocol. For our application we&#8217;ll just use the cookie-store to store the session information. </p>
<p>First step in creating the session is using the <em>wrap-session</em> function. We&#8217;ll add this to our routes and make sure that the session is wrapped for all the starting with &#8220;/admin&#8221;. Update your routes definition in core.clj as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Routes definition</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> routes
  <span style="color: #66cc66;">&#40;</span>app
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>file <span style="color: #ff0000;">&quot;resources/public&quot;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>params<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>session <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">cookie</span><span style="color: #66cc66;">-</span>name <span style="color: #ff0000;">&quot;clog-session&quot;</span> :<span style="color: #555;">store</span> <span style="color: #66cc66;">&#40;</span>cookie<span style="color: #66cc66;">-</span>store<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;login&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate login<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;admin&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate admin<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate index<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate post id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Also make sure that you add the necessary namespaces to the <em>core.clj</em> namespace.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> clog<span style="color: #66cc66;">.</span>core                           
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">use</span> ring<span style="color: #66cc66;">.</span>adapter<span style="color: #66cc66;">.</span>jetty 
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>resource
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>reload
        ring<span style="color: #66cc66;">.</span>util<span style="color: #66cc66;">.</span>response
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>file
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>params
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>session
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>session<span style="color: #66cc66;">.</span>cookie
        net<span style="color: #66cc66;">.</span>cgrand<span style="color: #66cc66;">.</span>moustache
        clog<span style="color: #66cc66;">.</span>controller<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>When a user logs in, we can use the session to store the username, and in subsequent requests to admin pages, we&#8217;ll verify if the session contains the username otherwise we send the user back to login page. First let us update the <em>login</em> method in the controller to make sure that we set the &#8220;username&#8221; on the session cookie.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> login
  <span style="color: #ff0000;">&quot;Login Handler&quot;</span>
  <span style="color: #66cc66;">&#91;</span>req<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>params <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">params</span> req<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">empty?</span> params<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>response <span style="color: #66cc66;">&#40;</span>login<span style="color: #66cc66;">-</span>page<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>get params <span style="color: #ff0000;">&quot;username&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>get params <span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> <span style="color: #66cc66;">&#40;</span>redirect <span style="color: #ff0000;">&quot;/admin&quot;</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #555;">session</span> <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">username</span> <span style="color: #66cc66;">&#40;</span>get params <span style="color: #ff0000;">&quot;username&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>response <span style="color: #66cc66;">&#40;</span>login<span style="color: #66cc66;">-</span>page <span style="color: #ff0000;">&quot;Invalid username or password&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>When the username is valid according to our logic &#8211; we set the session map during redirect. This will set a cookie with the name &#8220;clog-session&#8221; when the user is logged in.  You can check the cookie in your browser&#8217;s inspector.</p>
<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/02/session-cookie.png" class="enlarge"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/02/session-cookie-620x143.png" alt="" title="session-cookie" width="620" height="143" class="aligncenter size-large wp-image-3809" /></a></p>
<p>Now we&#8217;ll tweak the admin handler to check whether the username is set on the session cookie, if not, we&#8217;ll redirect the user to the login page.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> admin
  <span style="color: #ff0000;">&quot;Admin handler&quot;</span>
  <span style="color: #66cc66;">&#91;</span>req<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>username <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">username</span> <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">session</span> req<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil?</span> username<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>redirect <span style="color: #ff0000;">&quot;/login&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>response <span style="color: #ff0000;">&quot;Admin Page&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The next step is to add a logout function that will clear the session to make sure the username key is removed from the session.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #b1b100;">defn</span> logout
  <span style="color: #ff0000;">&quot;Logout handler&quot;</span>
  <span style="color: #66cc66;">&#91;</span>req<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> <span style="color: #66cc66;">&#40;</span>redirect <span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #555;">session</span> nil<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We also need to update our routes to make sure that&#8221;/logout&#8221; works as expected. Just add the new logout handler to the routes in core.clj as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> routes
  <span style="color: #66cc66;">&#40;</span>app
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>file <span style="color: #ff0000;">&quot;resources/public&quot;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>params<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>session <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">cookie</span><span style="color: #66cc66;">-</span>name <span style="color: #ff0000;">&quot;clog-session&quot;</span> :<span style="color: #555;">store</span> <span style="color: #66cc66;">&#40;</span>cookie<span style="color: #66cc66;">-</span>store<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;login&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate login<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;logout&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate logout<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;admin&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate admin<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate index<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate post id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>After these changes, when you try to access &#8220;/admin&#8221; without logging in you&#8217;ll be redirected to the login page as expected. Browsing to &#8220;/logout&#8221; page will &#8220;clear&#8221; your session redirecting you back to the home page of the blog.</p>
<p>Since we are now having only one &#8220;secure&#8221; page, we just added the session check to the admin handler function itself. Clearly this isn&#8217;t a nice way, since in a production version of your blog, you might have many more admin pages such as user settings, post settings etc. This approach of verifying the session in every function look pretty stupid in that case.</p>
<p>You can move the session checking logic to a different security wrapper handler which will check the request before forwarding to the next handler. If this security wrapper handler sees that the url is one of the &#8220;secure&#8221; urls, it will verify the session. When the session doesn&#8217;t contain the username key, it can redirect to the login page.</p>
<h2>Blog post admin page</h2>
<p>Now we have our simple security in place, let us finish our blog posting function that will allow us to create a new post. First create the admin html page which contains the form for the blog post.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Clog - Admin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;/bootstrap/js/html5.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;/bootstrap/bootstrap.css&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;style</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        body {
            padding-top: 80px;
        }
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;navbar navbar-fixed-top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;navbar-inner&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;container&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;brand&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;b<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Clog - The clojure Blog<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/b<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;pull-right&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;/logout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Logout<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;container&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>New Post<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;span8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;form&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;POST&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;/admin&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;control-group&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;control-label&quot;</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;title&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Title<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;title&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;title&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;span8 input-xlarge&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;control-group&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;control-label&quot;</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Content<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textarea</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;content&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;span8 input-xlarge&quot;</span> <span style="color: #000066;">rows</span>=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;form-actions&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn btn-primary&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Save<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The admin page contains a simple form with title field and a text area for the post content. We also added a logout link in case the user wants to logout of the admin area. The above html should be placed into <em>clog/resources/admin.html</em> along with other templates.</p>
<p>Update the <em>templates.clj</em> and create a oneliner that will use this template.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>deftemplate admin<span style="color: #66cc66;">-</span>page <span style="color: #ff0000;">&quot;admin.html&quot;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now replace the <em>admin</em> handler in <em>controller.clj</em> so that it will render the admin template as response.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> admin
  <span style="color: #ff0000;">&quot;Admin handler&quot;</span>
  <span style="color: #66cc66;">&#91;</span>req<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>username <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">username</span> <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">session</span> req<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil?</span> username<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>redirect <span style="color: #ff0000;">&quot;/login&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>response <span style="color: #66cc66;">&#40;</span>admin<span style="color: #66cc66;">-</span>page<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As you can see in the admin html, the form is posting the content to the same <em>admin</em> URL. So we need to capture that form parameters and update the database with the post details.</p>
<div class="asideBlock">Clearly this code looks super-contrived, but this is just to make things super simple and as straight forward as possible. You should use more idiomatic clojure to make this code look good.</div>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> admin
  <span style="color: #ff0000;">&quot;Admin handler&quot;</span>
  <span style="color: #66cc66;">&#91;</span>req<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>username <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">username</span> <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">session</span> req<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        params <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">params</span> req<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil?</span> username<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>redirect <span style="color: #ff0000;">&quot;/login&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if-not</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">empty?</span> params<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>id <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">inc</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">count</span> <span style="color: #66cc66;">&#40;</span>select posts<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                author<span style="color: #66cc66;">-</span>id <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">id</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">first</span> <span style="color: #66cc66;">&#40;</span>select authors <span style="color: #66cc66;">&#40;</span>fields :<span style="color: #555;">id</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>where <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">username</span> username<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
            <span style="color: #66cc66;">&#40;</span>insert posts <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> params
                                    :<span style="color: #555;">id</span> id
                                    :<span style="color: #555;">author</span> author<span style="color: #66cc66;">-</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>response <span style="color: #66cc66;">&#40;</span>admin<span style="color: #66cc66;">-</span>page<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Once this function is in, you can now login to the blog admin area using jane or john and add a new post to the blog. </p>
<h2>Conclusion</h2>
<p>This concludes the part 5 of this series. In the next and final part, I&#8217;ll discuss what sort of deployment options available and how you can deploy your Clog engine to &#8220;production&#8221;. As you can see the webapp we built so far is no where near to a production standard application, but this should help you get started with developing your own apps. You can now enhance clog with list of posts, deleting posts, adding comments etc. by just using the conceptual framework presented in this series. If you have any questions, feel free to ask in the comments of this post.</p>
<p>Make sure you <a href="http://vijaykiran.com/feed">subscribe</a> to the RSS feed or follow me on <a href="http://twitter.com/vijaykiran">Twitter</a> to get notified.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/EgwmzBoWUbaaU9JcH3KCD_Nuot8/0/da"><img src="http://feedads.g.doubleclick.net/~a/EgwmzBoWUbaaU9JcH3KCD_Nuot8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/EgwmzBoWUbaaU9JcH3KCD_Nuot8/1/da"><img src="http://feedads.g.doubleclick.net/~a/EgwmzBoWUbaaU9JcH3KCD_Nuot8/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=zjk_79dAPYA:RA8k36aF_vQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=zjk_79dAPYA:RA8k36aF_vQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=zjk_79dAPYA:RA8k36aF_vQ:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/zjk_79dAPYA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/02/27/web-application-development-with-clojure-part-5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/02/27/web-application-development-with-clojure-part-5/</feedburner:origLink></item>
		<item>
		<title>Web Application Development with Clojure – Part 4</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/Vip-3eqTXvo/</link>
		<comments>http://www.vijaykiran.com/2012/02/12/web-application-development-with-clojure-part-4/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 21:39:51 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[enlive]]></category>
		<category><![CDATA[webapplication]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3757</guid>
		<description><![CDATA[The story so far - In the last 3 parts, we completed setting up the project for a simple blog engine, loading the test data and finally displaying the blog posts on the "home" page. In this post first we'll first add some nice CSS, using Twitter Bootstrap, to make it look a bit better and finish the blog post detail page. Next we'll add an authentication page for the blog - a simple login page that will check the credentials entered. ]]></description>
			<content:encoded><![CDATA[<p>This post is part of the Web Application Development with <a href="http://clojure.org">Clojure</a> tutorial. You might want to read the previous posts before this post for continuity&#8217;s sake.</p>
<ul>
<li><a href="http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/">Part 1: Project Setup</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/">Part 2: Data model definition with Lobos and Korma</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/31/web-application-development-with-clojure-part-3/">Part 3: Loading Fixtures with <a href="https://github.com/lancepantz/clj-yaml">clj-yaml</a> and HTML Templating with <a href="https://github.com/cgrand/enlive">Enlive</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/12/web-application-development-with-clojure-part-4/">Part 4: Adding CSS, Post detail page and Simple Authentication</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/27/web-application-development-with-clojure-part-5/">Part 5: Blog Administration Area</a></li>
</ul>
<h2>Introduction</h2>
<p>The story so far &#8211; In the last 3 parts, we completed setting up the project for a simple blog engine, loading the test data and finally displaying the blog posts on the &#8220;home&#8221; page. In this post first we&#8217;ll first add some nice CSS to make it look a bit better and finish the blog post detail page. Next we&#8217;ll add an authentication page for the blog.</p>
<h3>Source code on github</h3>
<p>The code for this series is now available on <a href="https://github.com/vijaykiran/clog">github</a> and the source code is tagged with part names. If you want to checkout the code for a specific part of this tutorial you can do so using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">git clone git://github.com/vijaykiran/clog.git
cd clog
git checkout part4</pre></div></div>

<h2>Static Resources</h2>
<p>To make our blog look a bit more nice, we need some CSS and styling. We&#8217;ll use twitter&#8217;s bootstrap CSS for styling. To serve the css files from the root path, we can use ring middleware file provides a function <em>wrap-files</em> which will serve the static resources. First create a folder called <em>public</em> in the <em>resources</em> folder. This will contain all the static stuff like images, CSS etc. Now download and copy the bootstrap folder to the newly created <em>public</em> folder. The folder structure in IntelliJ should look like the following:</p>
<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/02/public-folder.png"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/02/public-folder.png" alt="" title="public-folder" width="303" height="385" class="aligncenter size-full wp-image-3763" /></a></p>
<p>Update the code in <em>core.clj</em>&#8216;s <em>routes</em> and add the <em>wrap-file</em> as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> routes
  <span style="color: #66cc66;">&#40;</span>app
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>file <span style="color: #ff0000;">&quot;resources/public&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate index<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now update your HTML for the home page with the following code. The changes I made are adding the CSS declaration to the top, and restructuring HTML a bit.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Clog - The clojure Blog<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;/bootstrap/css/bootstrap.css&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;style</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
         body {
             padding-top: 80px;
         }
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;navbar navbar-fixed-top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;navbar-inner&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;container&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;brand&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;b<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Clog - The clojure Blog<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/b<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;container&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;post row&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;page-header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;title&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Title of the Post<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Post Content<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<div class="asideBlock">Whenever you change the template you need to compile the template.clj, to see the changes. This is a limitation of Enlive &#8211; which I think is being addressed. But for now, the work around is use
<pre>(use 'clog.templates :reload)</pre>
<p> in the REPL, that will reload the templates.</p></div>
<p>Here&#8217;s how the home page looks now.<br />
<a href="http://www.vijaykiran.com/wp-content/uploads/2012/02/blog-screenshot.png" class="enlarge"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/02/blog-screenshot-620x417.png" alt="" title="blog-screenshot" width="620" height="417" class="aligncenter size-large wp-image-3768" /></a></p>
<h3>Hacking LESS (Optional)</h3>
<p>If you are feeling adventurous, you can try editing the Bootstrap LESS and change the styling to your liking. I use less.js and the .less files directly, and experiment with changing the styles. Just download the latest less.js and keep it under bootstrap/js and replace the stylesheet link in the html&#8217;s head element to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet/less&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;/bootstrap/less/bootstrap.less&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;/bootstrap/js/less-1.2.1.min.js&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Here&#8217;s how the blog looks like with Clojure Colors</p>
<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/02/clojure-colors-blog.png" class="enlarge"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/02/clojure-colors-blog-620x453.png" alt="" title="clojure-colors-blog" width="620" height="453" class="aligncenter size-large wp-image-3772" /></a></p>
<h2>Post Detail Page</h2>
<p>Now we just have the home page, but it would be better if we show the single blog post page when someone clicks on the post title. So let us add that functionality. First we need a new route that will handle the post detail URLs. Whenever someone goes to http://host/{post_id} we&#8217;ll show the detail page of that post. So we repeat pretty much the same exercise we did for rendering the home page, i.e., creating a template (both html and a new deftemplate in templates.clj ), adding a controller function, and finally calling that function in updated routes. </p>
<p>Here&#8217;s the updated version of the routes declaration which adds a new handler to the route as shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Routes definition</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> routes
  <span style="color: #66cc66;">&#40;</span>app
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>file <span style="color: #ff0000;">&quot;resources/public&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate index<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate post id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>For the HTML of the post detail page, we&#8217;ll just reuse the home page, copy the html from <em>home.html</em> to <em>post.html</em>. Update the <em>templates.clj</em> with the deftemplate macro, the code is also similar to the home-page template macro.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>deftemplate post<span style="color: #66cc66;">-</span>page <span style="color: #ff0000;">&quot;post.html&quot;</span> <span style="color: #66cc66;">&#91;</span>post<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">title</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>content <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">str</span> <span style="color: #ff0000;">&quot;Clog - &quot;</span> <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">title</span> post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">span</span><span style="color: #66cc66;">.</span>title<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>content <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">title</span> post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">div</span><span style="color: #66cc66;">.</span>content<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>html<span style="color: #66cc66;">-</span>content <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">content</span> post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>And finally the controller method</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> post
  <span style="color: #ff0000;">&quot;Post details page handler&quot;</span>
  <span style="color: #66cc66;">&#91;</span>req id<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>postId <span style="color: #66cc66;">&#40;</span>Integer<span style="color: #66cc66;">/</span>parseInt id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-&gt;&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">first</span> <span style="color: #66cc66;">&#40;</span>select posts <span style="color: #66cc66;">&#40;</span>where <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">id</span> postId<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      post<span style="color: #66cc66;">-</span>page response<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>After these changes you should be able to see the post detail page by going to http://host:port/1 or http://host:port/2 </p>
<h3>Link to the Post pages</h3>
<p>We&#8217;ll now modify the home page template and link the title of the post to the corresponding detail page. First modify the home.html and change the title <em>span</em> to an <em>anchor</em>. Here&#8217;s the relevant part of the modified html:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;title&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Title of the Post<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now edit the <em>templates.clj</em> to add the href attribute to the post detail page. Since we need to add the href and modify the content of the <em>a</em> tag, we use the <em>do-></em> function to add the transformations. Here&#8217;s the updated home-page macro in templates.clj:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>deftemplate home<span style="color: #66cc66;">-</span>page  <span style="color: #ff0000;">&quot;home.html&quot;</span> <span style="color: #66cc66;">&#91;</span>posts<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">title</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>content <span style="color: #ff0000;">&quot;Clog - the clojure blog engine!&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">div</span><span style="color: #66cc66;">.</span>post<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>clone<span style="color: #66cc66;">-</span><span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#91;</span>post posts<span style="color: #66cc66;">&#93;</span>
                <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">a</span><span style="color: #66cc66;">.</span>title<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>do<span style="color: #66cc66;">-&gt;</span>
                             <span style="color: #66cc66;">&#40;</span>set<span style="color: #66cc66;">-</span>attr :<span style="color: #555;">href</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">str</span> <span style="color: #ff0000;">&quot;/&quot;</span> <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">id</span> post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                             <span style="color: #66cc66;">&#40;</span>content <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">title</span> post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">div</span><span style="color: #66cc66;">.</span>content <span style="color: #66cc66;">&#40;</span>html<span style="color: #66cc66;">-</span>content <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">content</span> post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If everything went well, once you reload the templates namespace using <em>(use &#8216;clog.templates :reload)</em> you should see the home page with titles of the posts linking to the post detail pages.</p>
<h2>Authentication</h2>
<p>The next step is to create an administration area which is only accessible after login, and will be used to create the blog posts. First we need to create our login.html which will contain a form that will allow users to login. Notice that there&#8217;s an error div, which we will use to show the error message when the user enters invalid username and password. Take the following code and put it in a file <em>login.html</em> under <em>resources</em></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Clog - Login<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;/bootstrap/css/bootstrap.css&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;style</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
         body {
             padding-top: 80px;
         }
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;navbar navbar-fixed-top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;navbar-inner&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;container&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;brand&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;b<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Clog - The clojure Blog<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/b<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;container&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;span8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;error&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;alert alert-error&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;form&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;POST&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;/login&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;control-group&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;control-label&quot;</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;username&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Username<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;input-xlarge&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;control-group&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;control-label&quot;</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Password<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;input-xlarge&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;form-actions&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn btn-primary&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Login<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Cancel<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now let us add a macro to our <em>templates.clj</em> to render the login page. In this macro, we are checking if any message is passed to the template method, we&#8217;ll set the appropriate message in the error div. We are also using the <em>set-attr</em> and <em>remove-attr</em> functions from enlive.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>deftemplate login<span style="color: #66cc66;">-</span>page <span style="color: #ff0000;">&quot;login.html&quot;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&amp;</span> msg<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">div</span>#error<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil?</span> msg<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>set<span style="color: #66cc66;">-</span>attr :<span style="color: #555;">style</span> <span style="color: #ff0000;">&quot;display:none&quot;</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>do<span style="color: #66cc66;">-&gt;</span>
                    <span style="color: #66cc66;">&#40;</span>remove<span style="color: #66cc66;">-</span>attr :<span style="color: #555;">style</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#40;</span>content msg<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now let us update the routes, so when someone goes to <em>/login</em> we&#8217;ll render the login page. We need to also add a <em>POST</em> handler that will process the login form&#8217;s post request. We&#8217;ll also use the ring&#8217;s params middleware function <em>wrap-params</em> that will wrap the request parameters and pass it on along with request. So update the <em>clog.core</em> namespace to include the <em>ring.middleware.param</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> clog<span style="color: #66cc66;">.</span>core
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">use</span> ring<span style="color: #66cc66;">.</span>adapter<span style="color: #66cc66;">.</span>jetty
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>resource
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>reload
        ring<span style="color: #66cc66;">.</span>util<span style="color: #66cc66;">.</span>response
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>file
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>params
        net<span style="color: #66cc66;">.</span>cgrand<span style="color: #66cc66;">.</span>moustache
        clog<span style="color: #66cc66;">.</span>controller<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now update the routes var with wrap-params and handler for the login page and login action as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> routes
  <span style="color: #66cc66;">&#40;</span>app
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>params<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>wrap<span style="color: #66cc66;">-</span>file <span style="color: #ff0000;">&quot;resources/public&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;login&quot;</span><span style="color: #66cc66;">&#93;</span>  <span style="color: #66cc66;">&#40;</span>delegate login<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate index<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate post id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The login route will handle both POST and GET, since we haven&#8217;t specified any request method. The function <em>login</em> is in the controller.clj which is shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> login
  <span style="color: #ff0000;">&quot;Login Handler&quot;</span>
  <span style="color: #66cc66;">&#91;</span>req<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>params <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">params</span> req<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">empty?</span> params<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>response <span style="color: #66cc66;">&#40;</span>login<span style="color: #66cc66;">-</span>page<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>get params <span style="color: #ff0000;">&quot;username&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>get params <span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>redirect <span style="color: #ff0000;">&quot;/admin&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>response <span style="color: #66cc66;">&#40;</span>login<span style="color: #66cc66;">-</span>page <span style="color: #ff0000;">&quot;Invalid username or password&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is a bit &#8220;crude&#8221; way of handling login, but we are trying to keep this as simple as possible. What the above function is doing is checking if the params are empty, they&#8217;ll be empty when there&#8217;s a <em>GET</em> request on the <em>/login</em> URL, then just render the login-page with no parameters. When the params are not empty, check if username is equal to password, then redirect to the <em>/admin</em> url (which doesn&#8217;t exist yet, but we will add it soon). And finally, if the username isn&#8217;t equal to password, then render the same login-page but with a message <em>&#8220;Invalid username or password&#8221;</em>.</p>
<div class="asideBlock">There&#8217;s bug here in the implementation that you can just click &#8220;Login&#8221; to get to the /admin, which is because empty strings are equal according to the logic. Fixing the bug is left as exercise to the reader!</div>
<p>Now that all the required pieces are in place, when you start the server (with port 8080), you should be able to see the login page at http://localhost:8080/login and when you enter two different strings for username and password, you should see an error message. You&#8217;ll get a redirect to <em>/admin</em> when you enter same string for username and password.</p>
<h2>Conclusion</h2>
<p>In this part we finished the &#8220;front-end&#8221; of the blog and started working on the admin area. In the Part 5of this series, we&#8217;ll wrap up the blog engine by completing the administration area that allows you to create the blog posts. In sixth and final part of this series we&#8217;ll see what deployment options are available and how to deploy the blog engine on Heroku. </p>
<p>Make sure you <a href="http://vijaykiran.com/feed">subscribe</a> to the RSS feed or follow me on <a href="http://twitter.com/vijaykiran">Twitter</a> to get notified.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/lpLV5NlGiIeUuXRNNtM3XLdBe98/0/da"><img src="http://feedads.g.doubleclick.net/~a/lpLV5NlGiIeUuXRNNtM3XLdBe98/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/lpLV5NlGiIeUuXRNNtM3XLdBe98/1/da"><img src="http://feedads.g.doubleclick.net/~a/lpLV5NlGiIeUuXRNNtM3XLdBe98/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=Vip-3eqTXvo:KQaucg-OAM0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=Vip-3eqTXvo:KQaucg-OAM0:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=Vip-3eqTXvo:KQaucg-OAM0:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/Vip-3eqTXvo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/02/12/web-application-development-with-clojure-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/02/12/web-application-development-with-clojure-part-4/</feedburner:origLink></item>
		<item>
		<title>Web Application Development with Clojure – Part 3</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/tiZP28prIVQ/</link>
		<comments>http://www.vijaykiran.com/2012/01/31/web-application-development-with-clojure-part-3/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 15:57:18 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[enlive]]></category>
		<category><![CDATA[korma]]></category>
		<category><![CDATA[webapp]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3691</guid>
		<description><![CDATA[This post is part of the Web Application Development with Clojure tutorial. You might want to read the previous posts before this post for continuity&#8217;s sake. Part 1: Project Setup Part 2: Data model definition with Lobos and Korma Part 3: Loading Fixtures with clj-yaml and HTML Templating with Enlive Part 4: Adding CSS, Post [...]]]></description>
			<content:encoded><![CDATA[<p>This post is part of the Web Application Development with <a href="http://clojure.org">Clojure</a> tutorial. You might want to read the previous posts before this post for continuity&#8217;s sake.</p>
<ul>
<li><a href="http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/">Part 1: Project Setup</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/">Part 2: Data model definition with Lobos and Korma</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/31/web-application-development-with-clojure-part-3/">Part 3: Loading Fixtures with clj-yaml and HTML Templating with Enlive</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/12/web-application-development-with-clojure-part-4/">Part 4: Adding CSS, Post detail page and Simple Authentication</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/27/web-application-development-with-clojure-part-5/">Part 5: Blog Administration Area</a></li>
</ul>
<h2>Introduction</h2>
<p>In the <a title="Clojure Web App Development - Part 2" href="http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/">previous part</a> of this tutorial we created a couple of tables using migrations &#8211; authors table and posts table. In this part first I&#8217;ll describe how to load some test data (a.k.a &#8216;<em>fixtures</em>&#8216;) into our database tables. We&#8217;ll use YAML to define a couple of authors and 3 blog posts and we&#8217;ll parse the YAML file and load the data using the clj-yaml library.</p>
<p>Also we&#8217;ll create our first template and render the blog&#8217;s home page with the sample data from the database.</p>
<h3>Source code on github</h3>
<p>The code for this series is now available on <a href="https://github.com/vijaykiran/clog">github</a> and the source code is tagged with part names. If you want to checkout the code for a specific part of this tutorial you can do so using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">git clone git://github.com/vijaykiran/clog.git
cd clog
git checkout part3</pre></div></div>

<p><h>A quick note for PostgreSQL users</h3>
<p>In the previous while creating the tables using migrations, I overlooked and made a mistake in naming the tables. If you are using PostgreSQL then the word &#8220;user&#8221; is a reserved word, so you should rename the table to authors &#8211; I&#8217;ve updated the code accordingly. </p>
<h2>Loading Fixtures</h2>
<p>Fixtures are used to populate the database with some initial data. Though fixtures are mostly used for loading test data we can also use fixtures to bootstrap the content. First let us create a file called <em>fixtures.yml</em> under <em>resources</em> folder. If you don&#8217;t have a resources folder in your project, create it under the root directory so that will be at the same level as <em>src</em> folder.</p>
<p>Edit the <em>fixtures.yml</em> and add the following:</p>
<div class="asideBlock">Pay attention to the formatting of the text in a YAML file, otherwise you might get exceptions during parsing.</div>

<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;">authors:
    - id: 1
      username:     &quot;john&quot;
      password:     &quot;clojure-blogger&quot;
      email:        &quot;john@clojure-blog.org&quot;
    - id: 2
      username:     &quot;jane&quot;
      password:     &quot;clojure-blogher&quot;
      email:        &quot;jane@clojure-blog.org&quot;
&nbsp;
posts:
    - title: &quot;Hello World!&quot;
      text: &quot;&lt;em&gt;Welcome to Clog, the World's most advanced Clojure Blog Engine!&lt;/em&gt;&quot;
      status: true
      author: 1
    - title: &quot;Hello World!, again!&quot;
      text: &quot;Sayin 't 'gain! &lt;em&gt;Welcome to Clog, the World's most advanced Clojure Blog Engine!&lt;/em&gt;&quot;
      status: true
      author: 2</pre></div></div>

<div class="asideBlock>Check out <a title="Clojure Web App Development - Part 1" href="http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/">part 1</a>, if you want to refresh your memory on how to use setup IntelliJ IDE</div>
<p>Now, fire up the Clojure REPL in IntelliJ IDE using <em>Tools -> Start Clojure Console</em>. We&#8217;ll load the <em>clj-yaml</em> library and parse the yaml file.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; load clj-yaml</span>
<span style="color: #66cc66;">&#40;</span>use 'clj<span style="color: #66cc66;">-</span>yaml<span style="color: #66cc66;">.</span>core<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>parse<span style="color: #66cc66;">-</span>string <span style="color: #66cc66;">&#40;</span>slurp <span style="color: #ff0000;">&quot;./resources/fixtures.yml&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If everything went well, you should see the map with keys <em>:authors</em> and <em>:posts</em> in the console as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">authors</span> 
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">id</span> <span style="color: #cc66cc;">1</span>, :<span style="color: #555;">username</span> <span style="color: #ff0000;">&quot;john&quot;</span>, :<span style="color: #555;">password</span> <span style="color: #ff0000;">&quot;clojure-blogger&quot;</span>, :<span style="color: #555;">email</span> <span style="color: #ff0000;">&quot;john@clojure-blog.org&quot;</span><span style="color: #66cc66;">&#125;</span> 
   <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">id</span> <span style="color: #cc66cc;">2</span>, :<span style="color: #555;">username</span> <span style="color: #ff0000;">&quot;jane&quot;</span>, :<span style="color: #555;">password</span> <span style="color: #ff0000;">&quot;clojure-blogher&quot;</span>, :<span style="color: #555;">email</span> <span style="color: #ff0000;">&quot;jane@clojure-blog.org&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>, 
 :<span style="color: #555;">posts</span> 
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">title</span> <span style="color: #ff0000;">&quot;Hello World!&quot;</span>, 
    :<span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;&lt;i&gt;Welcome to Clog, the World's most advanced Clojure Blog Engine!&lt;/i&gt;&quot;</span>, 
    :<span style="color: #555;">status</span> true, :<span style="color: #555;">author</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#125;</span> 
   <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">title</span> <span style="color: #ff0000;">&quot;Hello World!, again!&quot;</span>, 
     :<span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;Sayin 't 'gain! &lt;i&gt;Welcome to Clog, the World's most advanced Clojure Blog Engine!&lt;/i&gt;&quot;</span>, 
    :<span style="color: #555;">status</span> true, :<span style="color: #555;">author</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now to add the authors to the database all you need is the following couple of lines:</p>
<div class="asideBlock">As you can see, you don&#8217;t need to write the code in a file, compile, redeploy and refresh to modify your program. All you need is a REPL which you use to build your program bottom-up &#8211; one little function at a time.</div>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;;load korma library and models</span>
<span style="color: #66cc66;">&#40;</span>use 'korma<span style="color: #66cc66;">.</span>db 'korma<span style="color: #66cc66;">.</span>core 'clog<span style="color: #66cc66;">.</span>models<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>insert authors <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">authors</span> <span style="color: #66cc66;">&#40;</span>parse<span style="color: #66cc66;">-</span>string <span style="color: #66cc66;">&#40;</span>slurp <span style="color: #ff0000;">&quot;./resources/fixtures.yml&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The second line in the above code snippet reads the fixtures file using <em>slurp</em>, parses the yaml using <em>parse-string</em> which returns a map with keys <em>:authors</em> and <em>:posts</em>. We use clojure&#8217;s maps-as-functions-of-keys features to get the list of authors and pass them to Korma&#8217;s insert function. Once this is executed you can see the data is now inserted into the Users table.</p>
<div class="asideBlock">In a real-world-application you never use password, but instead you will  probably store some encrypted hash. I&#8217;ll leave that as an exercise to the reader (not the clojure reader!).</div>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>select authors<span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;;output</span>
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">id</span> <span style="color: #cc66cc;">1</span>, :<span style="color: #555;">username</span> <span style="color: #ff0000;">&quot;john&quot;</span>, :<span style="color: #555;">password</span> <span style="color: #ff0000;">&quot;clojure-blogger&quot;</span>, :<span style="color: #555;">email</span> <span style="color: #ff0000;">&quot;john@clojure-blog.org&quot;</span><span style="color: #66cc66;">&#125;</span> 
 <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">id</span> <span style="color: #cc66cc;">2</span>, :<span style="color: #555;">username</span> <span style="color: #ff0000;">&quot;jane&quot;</span>, :<span style="color: #555;">password</span> <span style="color: #ff0000;">&quot;clojure-blogher&quot;</span>, :<span style="color: #555;">email</span> <span style="color: #ff0000;">&quot;jane@clojure-blog.org&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#40;</span>select posts<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">....</span></pre></div></div>

<h2>Templates using Enlive</h2>
<p>Now that we have our sample data populated in the database, it is time to get started with our first template. Clojure has a couple of libraries that can help you with dealing with HTML viz, Hiccup and Enlive. Hiccup is a DSL for generating HTML and Enlive is a selector based templating engine. I&#8217;m a bit biased towards Enlive &#8211; since I have more freedom of styling and HTML stays HTML. So in this tutorial I&#8217;m using Enlive.</p>
<h3> Defining Home Page Template</h3>
<p>Enlive provides a macro called <em>deftemplate</em> to define template functions. The macro is defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> deftemplate
 <span style="color: #ff0000;">&quot;Defines a template as a function that returns a seq of strings.&quot;</span> 
 <span style="color: #66cc66;">&#91;</span>name source args <span style="color: #66cc66;">&amp;</span> forms<span style="color: #66cc66;">&#93;</span> 
  `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> ~name <span style="color: #66cc66;">&#40;</span>template ~source ~args ~@forms<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The forms you pass in are actually used to transform the html. I think it will be easier to understand with an example that you can follow along. First let us create the <em>home.html</em> under <em>resources</em> folder. By convention the templates are loaded from the resources directory.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Title Here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
Clog - Home Page!
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>We will place all our template definitions in <em>clog.templates</em> namespace which lives in cleverly named <em>templates.clj</em></p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> clog<span style="color: #66cc66;">.</span>templates
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">use</span> <span style="color: #66cc66;">&#91;</span>net<span style="color: #66cc66;">.</span>cgrand<span style="color: #66cc66;">.</span>enlive<span style="color: #66cc66;">-</span>html<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>deftemplate home<span style="color: #66cc66;">-</span>page <span style="color: #ff0000;">&quot;home.html&quot;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">title</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>content <span style="color: #ff0000;">&quot;Clog - the clojure blog engine!&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>In the above snippet we are using <em>deftemplate</em> macro to create a template function called <em>home-page</em> which takes the file <em>home.html</em> and transforms the file using the forms, in our case only one which is:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">title</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>content <span style="color: #ff0000;">&quot;Clog - the clojure blog engine!&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This says find out the tag :title in the home.html and replace its contents with &#8220;Clog &#8211; the clojure blog engine!&#8221;. To test this out you can load the namespace in repl and run the <em>home-page</em> function. That will print out the transformed template as shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>use 'clog<span style="color: #66cc66;">.</span>templates<span style="color: #66cc66;">&#41;</span>
nil
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>home<span style="color: #66cc66;">-</span>page<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&lt;!DOCTYPE html&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #ff0000;">&quot;&lt;html&gt;&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #ff0000;">&quot;&lt;head&gt;&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>    &quot;</span> <span style="color: #ff0000;">&quot;&lt;title&gt;&quot;</span> <span style="color: #ff0000;">&quot;Clog - the clojure blog engine!&quot;</span> <span style="color: #ff0000;">&quot;&lt;/title&gt;&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #ff0000;">&quot;&lt;/head&gt;&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #ff0000;">&quot;&lt;body&gt;<span style="color: #000099; font-weight: bold;">\n</span>Clog - Home Page!<span style="color: #000099; font-weight: bold;">\n</span>&lt;/body&gt;&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #ff0000;">&quot;&lt;/html&gt;&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now this template function can be used to generate the transformed html we need to send to the client. To connect the these views to the requests, we will use simple controller functions. These functions/handlers are then used to transform the templates using the request parameter and data from the database. For example we want our blog to respond to something like <em>http://host:port/posts/1</em> by sending out the html for the post with id 1. This logic is wrapped inside a controller function.</p>
<h3>A simple Controller</h3>
<p>We&#8217;ll put our controller functions in the <em>clog.controller</em> namespace. Here&#8217;s the initial version of the controller which has handler for the index page.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> clog<span style="color: #66cc66;">.</span>controller
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">use</span> clog<span style="color: #66cc66;">.</span>templates
         clog<span style="color: #66cc66;">.</span>models
         ring<span style="color: #66cc66;">.</span>util<span style="color: #66cc66;">.</span>response<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> index
  <span style="color: #ff0000;">&quot;Index page handler&quot;</span>
  <span style="color: #66cc66;">&#91;</span>req<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-&gt;&gt;</span> <span style="color: #66cc66;">&#40;</span>home<span style="color: #66cc66;">-</span>page<span style="color: #66cc66;">&#41;</span> response<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; A sexier way to write (response (home-page))</span></pre></div></div>

<p>Now to see this function in action, we need to update the routes of the application in core.clj to call clog.controller/index. Here&#8217;s the complete core.clj after the required modifications:</p>
<div class="asideBlock">Notice that <em>delegate</em> call in routes ? This is due to small gotcha in mustache route syntax. We can&#8217;t use the function directly as the handler, since we want the parameters of the handler, in our case &#8216;req&#8217; to be passed to function. So we use delegate to pass the request as the first argument.</div>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> clog<span style="color: #66cc66;">.</span>core
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">use</span> ring<span style="color: #66cc66;">.</span>adapter<span style="color: #66cc66;">.</span>jetty
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>resource
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>reload
        ring<span style="color: #66cc66;">.</span>util<span style="color: #66cc66;">.</span>response
        net<span style="color: #66cc66;">.</span>cgrand<span style="color: #66cc66;">.</span>moustache
        clog<span style="color: #66cc66;">.</span>controller<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Routes definition</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> routes
  <span style="color: #66cc66;">&#40;</span>app
    <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>delegate index<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;;; start function for starting jetty</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> start <span style="color: #66cc66;">&#91;</span>port<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>run<span style="color: #66cc66;">-</span>jetty #'routes <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">port</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> port <span style="color: #cc66cc;">8080</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #555;">join</span>? false<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> <span style="color: #66cc66;">-</span>main <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>port <span style="color: #66cc66;">&#40;</span>Integer<span style="color: #66cc66;">/</span>parseInt <span style="color: #66cc66;">&#40;</span>System<span style="color: #66cc66;">/</span>getenv <span style="color: #ff0000;">&quot;PORT&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span>start port<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Start the server as shown below, and you should see the home page with title in the window title bar &#8220;Clog &#8211; the clojure blog engine!&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>use 'clog<span style="color: #66cc66;">.</span>core<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>start <span style="color: #cc66cc;">9000</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>List of Posts</h3>
<p>The home is working, but it is rather boring and doesn&#8217;t show any real data. Let us try to show the two posts we added in the database. First we need to update the home.html to create a place holder for a post.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Title Here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;post&quot;&gt;
    &lt;div class=&quot;title&quot;&gt;Title of the Post&lt;/div&gt;
    &lt;div class=&quot;content&quot;&gt;Post Content&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Now let us update the template and add another transformation. In this case we want to &#8220;clone&#8221; the <em>div</em> with class <em>post</em> and replace the content of divs with classes <em>title</em> and <em>content</em>. Here&#8217;s the updated deftemplate&#8217;s code that does exactly the same. We are using clone-for macro and using the forms to transform the sub elements.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>deftemplate home<span style="color: #66cc66;">-</span>page  <span style="color: #ff0000;">&quot;home.html&quot;</span> <span style="color: #66cc66;">&#91;</span>posts<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">title</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>content <span style="color: #ff0000;">&quot;Clog - the clojure blog engine!&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">div</span><span style="color: #66cc66;">.</span>post<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>clone<span style="color: #66cc66;">-</span><span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#91;</span>post posts<span style="color: #66cc66;">&#93;</span>
                <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">div</span><span style="color: #66cc66;">.</span>title<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>content <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">title</span> post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#91;</span>:<span style="color: #555;">div</span><span style="color: #66cc66;">.</span>content <span style="color: #66cc66;">&#40;</span>content <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">content</span> post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now we need to make sure that our controller function sends the required data to the template.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> clog<span style="color: #66cc66;">.</span>controller
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">use</span>  clog<span style="color: #66cc66;">.</span>templates
         clog<span style="color: #66cc66;">.</span>models
         ring<span style="color: #66cc66;">.</span>util<span style="color: #66cc66;">.</span>response
         korma<span style="color: #66cc66;">.</span>core<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> index
  <span style="color: #ff0000;">&quot;Index page handler&quot;</span>
  <span style="color: #66cc66;">&#91;</span>req<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-&gt;&gt;</span> <span style="color: #66cc66;">&#40;</span>select posts<span style="color: #66cc66;">&#41;</span> home<span style="color: #66cc66;">-</span>page response<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; Equivalent to  (response (home-page (select posts))</span></pre></div></div>

<p>We are selecting all the posts using Korma select function and passing it on to home-page function. The result of the home-page template function &#8211; which is the html with posts populated is passed on to response.</p>
<p>After making these changes, you should see the list of posts in the home page.</p>
<div class="asideBlock">Don&#8217;t worry about styling of the page, we&#8217;ll add a pretty CSS in the next part of the tutorial.</div>
<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/01/posts.png" class="enlarge"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/01/posts-620x442.png" alt="" title="posts" width="620" height="442" class="aligncenter size-large wp-image-3742" /></a></p>
<h3>Conclusion</h3>
<p>In this part we saw how to glue things together and get the content from database, render it in a page using templates. In the next part we&#8217;ll finish by updating the home template and adding another template to display the post. Also, in the next part we&#8217;ll start working in creating an admin area with authentication. </p>
<p>Make sure you <a href="http://vijaykiran.com/feed">subscribe</a> to the RSS feed or follow me on <a href="http://twitter.com/vijaykiran">Twitter</a> to get notified.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/GpLg40FCZaZyCbk0XzC_8siWi4Q/0/da"><img src="http://feedads.g.doubleclick.net/~a/GpLg40FCZaZyCbk0XzC_8siWi4Q/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/GpLg40FCZaZyCbk0XzC_8siWi4Q/1/da"><img src="http://feedads.g.doubleclick.net/~a/GpLg40FCZaZyCbk0XzC_8siWi4Q/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=tiZP28prIVQ:YwxFfZnAz6U:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=tiZP28prIVQ:YwxFfZnAz6U:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=tiZP28prIVQ:YwxFfZnAz6U:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/tiZP28prIVQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/01/31/web-application-development-with-clojure-part-3/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/01/31/web-application-development-with-clojure-part-3/</feedburner:origLink></item>
		<item>
		<title>Photo: Muffins with Raisins</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/TZAYzmcIJno/</link>
		<comments>http://www.vijaykiran.com/2012/01/22/photo-muffins-with-raisins/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 11:23:34 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3713</guid>
		<description />
			<content:encoded><![CDATA[<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/01/20120122-122125.jpg"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/01/20120122-122125.jpg" alt="20120122-122125.jpg" class="alignnone size-full" /></a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/yuwypilySBDcxIBmdRtqc9ORoOU/0/da"><img src="http://feedads.g.doubleclick.net/~a/yuwypilySBDcxIBmdRtqc9ORoOU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/yuwypilySBDcxIBmdRtqc9ORoOU/1/da"><img src="http://feedads.g.doubleclick.net/~a/yuwypilySBDcxIBmdRtqc9ORoOU/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=TZAYzmcIJno:CRLjpxiMM1o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=TZAYzmcIJno:CRLjpxiMM1o:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=TZAYzmcIJno:CRLjpxiMM1o:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/TZAYzmcIJno" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/01/22/photo-muffins-with-raisins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/01/22/photo-muffins-with-raisins/</feedburner:origLink></item>
		<item>
		<title>My super cool workplace at Lunatech</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/FX9EDAkXs2Q/</link>
		<comments>http://www.vijaykiran.com/2012/01/19/my-desk-at-lunatech/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 18:02:27 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[lunatech]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3694</guid>
		<description><![CDATA[In my 10 years of programming career, Lunatech is probably the best company to work with. Herman Miller Aeron: Ultimate Programmer&#8217;s Chair Nerf Recon CS-6 with N-Strike Darts: For fun times. 17&#8243; Mac Book Pro with 8GB RAM, SSD running Emacs in Full Screen: Ultimate Programmer&#8217;s Computer The Joy of Clojure: The book for Ultimate [...]]]></description>
			<content:encoded><![CDATA[<p>In my 10 years of programming career, <a href="http://www.lunatech-research.com/">Lunatech</a> is probably the best company to work with.</p>
<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/01/lunatech-desk.png" class="enlarge"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/01/lunatech-desk-620x464.png" alt="" title="lunatech-desk" width="620" height="464" class="alignleft size-large wp-image-3695" /></a></p>
<ol>
<li>Herman Miller Aeron: Ultimate Programmer&#8217;s Chair </li>
<li>Nerf Recon CS-6 with N-Strike Darts: For fun times.</li>
<li>17&#8243; Mac Book Pro with 8GB RAM, SSD running Emacs in Full Screen: Ultimate Programmer&#8217;s Computer</li>
<li>The Joy of Clojure: The book for Ultimate programming language</li>
<li>mStand for MBP: For enhanced ergonomics and coolness factor</li>
</ol>
<p>Not shown in the photo:</p>
<ul>
<li>Super Awesome Colleagues</li>
<li>Projects using cutting-edge tech including: Scala, Play! Framework.</li>
<li>No Managers</li>
<li>No Managers, Seriously.</li>
<li>WORK-ON-YOUR-OWN-COOL-PROJECT-EVERY-FRIDAY &#8211; Yeah, EVERY Friday.</li>
</ul>
<p>Like what you see and curious about what we do ? Come and talk to us at <a href="http://playframework.eventbrite.com/">Play!ground</a> on February 3rd at Paddy Murphy’s Irish Pub, Rotterdam.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/sNu17yp6izhPshGEEn_jbcSm_xw/0/da"><img src="http://feedads.g.doubleclick.net/~a/sNu17yp6izhPshGEEn_jbcSm_xw/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/sNu17yp6izhPshGEEn_jbcSm_xw/1/da"><img src="http://feedads.g.doubleclick.net/~a/sNu17yp6izhPshGEEn_jbcSm_xw/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=FX9EDAkXs2Q:Tk57zO2AAbc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=FX9EDAkXs2Q:Tk57zO2AAbc:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=FX9EDAkXs2Q:Tk57zO2AAbc:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/FX9EDAkXs2Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/01/19/my-desk-at-lunatech/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/01/19/my-desk-at-lunatech/</feedburner:origLink></item>
		<item>
		<title>Web Application Development with Clojure – Part 2</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/PSYWiOnEVgU/</link>
		<comments>http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 23:00:50 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[korma]]></category>
		<category><![CDATA[lobos]]></category>
		<category><![CDATA[webapp]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3646</guid>
		<description><![CDATA[This post is part of the Web Application Development with Clojure tutorial. You may want to read the previous posts before continuing with this post. Part 1: Project Setup Part 2: Data model definition with Lobos and Korma Part 3: Loading Fixures with clj-yaml and HTML Templating with Enlive Part 4: Adding CSS, Post detail [...]]]></description>
			<content:encoded><![CDATA[<p>This post is part of the Web Application Development with <a href="http://clojure.org">Clojure</a> tutorial. You may want to read the previous posts before continuing with this post.</p>
<ul>
<li><a href="http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/">Part 1: Project Setup</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/">Part 2: Data model definition with Lobos and Korma</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/31/web-application-development-with-clojure-part-3/">Part 3: Loading Fixures with <a href="https://github.com/lancepantz/clj-yaml">clj-yaml</a> and HTML Templating with <a href="https://github.com/cgrand/enlive">Enlive</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/12/web-application-development-with-clojure-part-4/">Part 4: Adding CSS, Post detail page and Simple Authentication</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/27/web-application-development-with-clojure-part-5/">Part 5: Blog Administration Area</a></li>
</ul>
<p><em>Updated: 31st January, 2012 &#8211; Renamed all &#8216;users&#8217; to &#8216;authors&#8217; to prevent problems with PostgreSQL</em></p>
<h2>Introduction</h2>
<p>In <a href="http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/">part 1</a> of this series, we learnt how to setup a Clojure web application project &#8211; Clog a blogging engine. In this second part of the tutorial we will start working on the application&#8217;s database layer &#8211; setting up the schema, creating migrations and finally creating the entities for the model. This terminology should be familiar for people who already worked with other web application frameworks such as Ruby on Rails or Play! framework. If you have never used these frameworks, do not worry no knowledge of those is assumed in this tutorial.</p>
<h3>Source code on github</h3>
<p>The code for this series is now available on <a href="https://github.com/vijaykiran/clog">github</a> and the source code is tagged with part names. If you want to checkout the code for a specific part of this tutorial you can do so using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">git clone git://github.com/vijaykiran/clog.git
cd clog
git checkout part1</pre></div></div>

<h2>Database Schema Setup</h2>
<p>Normally when you want to work with a database, you will create a schema using DDL (Data Definition Language). DDL contains SQL statements like &#8220;CREATE TABLE AUTHOR …&#8221; etc. This will define the schema objects you need to store your application&#8217;s data. In this post however, I&#8217;d like to show you how you can create and manage the schema using a Clojure library called Lobos.</p>
<p>Lobos library provides a DSL (Domain Specific Language) that helps you in defining the database schema and migrations in a Database-agnostic way. By that I mean you can define your schema in Clojure and you can use Lobos to create the schema on any database e.g. PostgreSQL, MySQL or HSQL.</p>
<p>Before we begin, we need to create a database. As you might have seen in <a href="http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/">part 1</a>&#8216;s dependency listing, for this tutorial we will use PostgreSQL as the database.</p>
<h2>Database Setup</h2>
<div class="asideBlock">If you prefer MySQL database to PostgreSQL, you can replace the dependency jar with MySQL jdbc driver. But I&#8217;ll leave it as an exercise for the reader.</div>
<p>First install PostgreSQL for your platform by downloading it from <a href="http://www.postgresql.org/download/">here</a>. Create a user with login <em>clog</em> and your choice of password. Create a database named <em>clogdb</em> and make sure that the user has permissions on the database. To perform these steps you can either use the pgAdmin GUI tool to manage the database or you can use psql command line tool. If you prefer the latter, you can use the following commands to create the database and user:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">USER</span> clog <span style="color: #993333; font-weight: bold;">WITH</span> PASSWORD <span style="color: #ff0000;">'Your_Choice_Of_Password'</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> clogdb;
&nbsp;
<span style="color: #993333; font-weight: bold;">GRANT</span> <span style="color: #993333; font-weight: bold;">ALL</span> PRIVILEGES <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> clogdb <span style="color: #993333; font-weight: bold;">TO</span> clog;</pre></div></div>

<h2>Schema Creation using Lobos</h2>
<p>Before we start writing code in Clojure with Lobos, let us take a quick look at the terminology.</p>
<h3>Migrations</h3>
<p>Migrations are incremental steps that you have taken to evolve your database schema. Let me explain what a migration means with a concrete example. When you are creating the tables for our blog engine, you will create a table called <em>Posts</em> to store our blog posts. The <em>Posts</em> table might contain the following columns: <em>id</em>, <em>publishedDate</em>, <em>status</em>, <em>content</em>, <em>title</em>. To create a table with these fields you&#8217;ll use the following SQL DDL:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> posts <span style="color: #66cc66;">&#40;</span> id <span style="color: #993333; font-weight: bold;">INTEGER</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span><span style="color: #66cc66;">,</span> 
                     title text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
                     content text<span style="color: #66cc66;">,</span>
                     published_date DATETIME <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
                     <span style="color: #993333; font-weight: bold;">STATUS</span> text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> 
                     <span style="color: #993333; font-weight: bold;">CONSTRAINT</span> posts_id_key <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now at a later point of time you want to add rating to all the blog posts, that means you need to alter the schema to add a new column to the <em>Posts</em> table. This can be done using following Alter table statement.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> posts
      <span style="color: #993333; font-weight: bold;">ADD</span> rating <span style="color: #993333; font-weight: bold;">INTEGER</span>;</pre></div></div>

<p>In the above scenario, your database schema on the whole, has two versions or revisions. Version one that resulted when you executed the CREATE table statement and Version one when you executed the Alter table version. You can treat the two statements you used to modify the database as two <em>migrations</em>. Initially your database schema is at Version 0, once the first migration is executed, you have updated your database to version 1. After second statement, the ALTER TABLE one, is executed your database will be at version 2.</p>
<p>To &#8220;rollback&#8221; the schema you need a statement which will drop the added rating column, and another to drop the table completely which will result in database resulting in Version 0. When all these SQL statements are combined we get what we call &#8220;migrations&#8221;. Migrations help you &#8220;migrate&#8221; and &#8220;rollback&#8221; the database. Think of the migrations as a version control mechanism for your database.</p>
<p>Now that we know what migrations mean, let us see how to create the schema and migrations using <a href="http://budu.github.com/lobos/">Lobos</a>.</p>
<h3>Schema Definition using Lobos migrations</h3>
<p>We will define all the schema related operations and migrations in a Clojure namespace called migrations. First create a new directory called <em>lobos</em> under <em>src</em>. If you are using IntelliJ IDEA, then right click the <em>src</em> directory and select New &gt; Package and create a package named lobos. Inside the <em>src/lobos</em> create a file called <em>migrations.clj</em>. Add the following code to the file:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> lobos<span style="color: #66cc66;">.</span>migrations
  <span style="color: #808080; font-style: italic;">;; exclude some clojure built-in symbols so we can use the lobos' symbols</span>
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">refer</span><span style="color: #66cc66;">-</span>clojure :<span style="color: #555;">exclude</span> <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">alter</span> <span style="color: #b1b100;">drop</span>
                            bigint boolean char double float <span style="color: #b1b100;">time</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #808080; font-style: italic;">;; use only defmigration macro from lobos</span>
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">use</span> <span style="color: #66cc66;">&#40;</span>lobos <span style="color: #66cc66;">&#91;</span>migration :<span style="color: #555;">only</span> <span style="color: #66cc66;">&#91;</span>defmigration<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
          core
          schema<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;;; Defines the database for lobos migrations</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> clogdb
  <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">classname</span> <span style="color: #ff0000;">&quot;org.postgresql.Driver&quot;</span>
   :<span style="color: #555;">subprotocol</span> <span style="color: #ff0000;">&quot;postgresql&quot;</span>
   :<span style="color: #555;">subname</span> <span style="color: #ff0000;">&quot;clogdb&quot;</span>
   :<span style="color: #555;">user</span> <span style="color: #ff0000;">&quot;clog&quot;</span>
   :<span style="color: #555;">password</span> <span style="color: #ff0000;">&quot;clog&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The code snippet above defines a namespace migration with one var <em>clogdb</em> which is a map of database connection information. You may need to change the database password with the one you used while creating the database user.</p>
<p>We create our first migration that will add the users table:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defmigration add<span style="color: #66cc66;">-</span>authors<span style="color: #66cc66;">-</span>table
  <span style="color: #808080; font-style: italic;">;; code be executed when migrating the schema &quot;up&quot; using &quot;migrate&quot;</span>
  <span style="color: #66cc66;">&#40;</span>up <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>create clogdb
           <span style="color: #66cc66;">&#40;</span>table :<span style="color: #555;">authors</span> <span style="color: #66cc66;">&#40;</span>integer :<span style="color: #555;">id</span> :<span style="color: #555;">primary</span><span style="color: #66cc66;">-</span>key <span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>varchar :<span style="color: #555;">username</span> <span style="color: #cc66cc;">100</span> :<span style="color: #555;">unique</span> <span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>varchar :<span style="color: #555;">password</span> <span style="color: #cc66cc;">100</span> :<span style="color: #555;">not</span><span style="color: #66cc66;">-</span>null <span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>varchar :<span style="color: #555;">email</span> <span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #808080; font-style: italic;">;; Code to be executed when migrating schema &quot;down&quot; using &quot;rollback&quot;</span>
  <span style="color: #66cc66;">&#40;</span>down <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">drop</span> <span style="color: #66cc66;">&#40;</span>table :<span style="color: #555;">authors</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Next we define the migration that will create the posts table as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defmigration add<span style="color: #66cc66;">-</span>posts<span style="color: #66cc66;">-</span>table
  <span style="color: #66cc66;">&#40;</span>up <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>create clogdb
           <span style="color: #66cc66;">&#40;</span>table :<span style="color: #555;">posts</span> <span style="color: #66cc66;">&#40;</span>integer :<span style="color: #555;">id</span> :<span style="color: #555;">primary</span><span style="color: #66cc66;">-</span>key <span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>varchar :<span style="color: #555;">title</span> <span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>text :<span style="color: #555;">content</span> <span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>boolean :<span style="color: #555;">status</span> <span style="color: #66cc66;">&#40;</span>default false<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>timestamp :<span style="color: #555;">created</span> <span style="color: #66cc66;">&#40;</span>default <span style="color: #66cc66;">&#40;</span>now<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>timestamp :<span style="color: #555;">published</span> <span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>integer :<span style="color: #555;">author</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #b1b100;">refer</span> :<span style="color: #555;">authors</span> :<span style="color: #555;">id</span><span style="color: #66cc66;">&#93;</span> :<span style="color: #555;">not</span><span style="color: #66cc66;">-</span>null<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>down <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">drop</span> <span style="color: #66cc66;">&#40;</span>table :<span style="color: #555;">posts</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The above code is pretty much similar to the authors table migration &#8211; a title column, text column and a couple of timestamp columns to store the created and published date and times. Also, every post belongs to an author, so we create a foreign key to the authors table using :refer. Here we are referring to the :authors table&#8217;s :id column.</p>
<p>To run the above code, start a REPL console if it isn&#8217;t already running. Type the following commands into the console:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>use 'lobos<span style="color: #66cc66;">.</span>core 'lobos<span style="color: #66cc66;">.</span>connectivity 'lobos<span style="color: #66cc66;">.</span>migration 'lobos<span style="color: #66cc66;">.</span>migrations<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>open<span style="color: #66cc66;">-</span>global clogdb<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>migrate<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>You should see the following output in the console:</p>
<div class="asideBlock">Don&#8217;t worry about the warnings, they are just alerting you that you are replacing some core symbol references with the lobos&#8217; symbols.</div>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;">WARNING: <span style="color: #b1b100;">alter</span> already refers to: #'clojure<span style="color: #66cc66;">.</span>core<span style="color: #66cc66;">/</span><span style="color: #b1b100;">alter</span> in namespace: <span style="color: #555;">user</span>, being replaced by: #'lobos<span style="color: #66cc66;">.</span>core<span style="color: #66cc66;">/</span><span style="color: #b1b100;">alter</span>
WARNING: <span style="color: #b1b100;">drop</span> already refers to: #'clojure<span style="color: #66cc66;">.</span>core<span style="color: #66cc66;">/</span><span style="color: #b1b100;">drop</span> in namespace: <span style="color: #555;">user</span>, being replaced by: #'lobos<span style="color: #66cc66;">.</span>core<span style="color: #66cc66;">/</span><span style="color: #b1b100;">drop</span>
WARNING: <span style="color: #555;">complement</span> already refers to: #'clojure<span style="color: #66cc66;">.</span>core<span style="color: #66cc66;">/</span>complement in namespace: <span style="color: #555;">user</span>, being replaced by: #'lobos<span style="color: #66cc66;">.</span>migration<span style="color: #66cc66;">/</span>complement
nil
<span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">default</span><span style="color: #66cc66;">-</span>connection <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">connection</span> #, :<span style="color: #555;">db</span><span style="color: #66cc66;">-</span>spec <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">subprotocol</span> <span style="color: #ff0000;">&quot;postgresql&quot;</span>, :<span style="color: #555;">classname</span> <span style="color: #ff0000;">&quot;org.postgresql.Driver&quot;</span>, :<span style="color: #555;">subname</span> <span style="color: #ff0000;">&quot;clogdb&quot;</span>, :<span style="color: #555;">user</span> <span style="color: #ff0000;">&quot;clog&quot;</span>, :<span style="color: #555;">password</span> <span style="color: #ff0000;">&quot;clog&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span>
add<span style="color: #66cc66;">-</span>authors<span style="color: #66cc66;">-</span>table
add<span style="color: #66cc66;">-</span>posts<span style="color: #66cc66;">-</span>table
nil</pre></div></div>

<p>To cross check what relations are created in the database, you can use psql and try the following commands:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">clogdb<span style="color: #66cc66;">=</span># \d
\d         
             List <span style="color: #993333; font-weight: bold;">OF</span> relations              
 Schema <span style="color: #66cc66;">|</span>       Name       <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">TYPE</span>  <span style="color: #66cc66;">|</span> Owner  
<span style="color: #808080; font-style: italic;">--------+------------------+-------+------- </span>
 public <span style="color: #66cc66;">|</span> lobos_migrations <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">|</span> clog   
 public <span style="color: #66cc66;">|</span> posts            <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">|</span> clog   
 public <span style="color: #66cc66;">|</span> authors            <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">|</span> clog   
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #993333; font-weight: bold;">ROWS</span><span style="color: #66cc66;">&#41;</span>                                    
&nbsp;
clogdb<span style="color: #66cc66;">=</span># \d authors                                                                           
\d authors                                                                                    
             <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">&quot;public.authors&quot;</span>                                                           
  <span style="color: #993333; font-weight: bold;">COLUMN</span>  <span style="color: #66cc66;">|</span>          <span style="color: #993333; font-weight: bold;">TYPE</span>          <span style="color: #66cc66;">|</span> Modifiers                                              
<span style="color: #808080; font-style: italic;">----------+------------------------+-----------                                             </span>
 id       <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">INTEGER</span>                <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>                                               
 username <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">VARYING</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>                                                        
 password <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">VARYING</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>                                               
 email    <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">VARYING</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>                                                        
Indexes:                                                                                    
    <span style="color: #ff0000;">&quot;authors_primary_key_id&quot;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span> btree <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>                                          
    <span style="color: #ff0000;">&quot;authors_unique_username&quot;</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span><span style="color: #66cc66;">,</span> btree <span style="color: #66cc66;">&#40;</span>username<span style="color: #66cc66;">&#41;</span>                                        
Referenced <span style="color: #993333; font-weight: bold;">BY</span>:                                                                              
    <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">&quot;posts&quot;</span> <span style="color: #993333; font-weight: bold;">CONSTRAINT</span> <span style="color: #ff0000;">&quot;posts_fkey_user&quot;</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;author&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> authors<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>    
&nbsp;
clogdb<span style="color: #66cc66;">=</span># \d posts                                                                           
\d posts                                                                                    
                  <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">&quot;public.posts&quot;</span>                                                      
  <span style="color: #993333; font-weight: bold;">COLUMN</span>   <span style="color: #66cc66;">|</span>            <span style="color: #993333; font-weight: bold;">TYPE</span>             <span style="color: #66cc66;">|</span>   Modifiers                                      
<span style="color: #808080; font-style: italic;">-----------+-----------------------------+---------------                                   </span>
 id        <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">INTEGER</span>                     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>                                         
 title     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">VARYING</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #66cc66;">|</span>                                                  
 content   <span style="color: #66cc66;">|</span> text                        <span style="color: #66cc66;">|</span>                                                  
 <span style="color: #993333; font-weight: bold;">STATUS</span>    <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">BOOLEAN</span>                     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">FALSE</span>                                    
 created   <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">TIMESTAMP</span> <span style="color: #993333; font-weight: bold;">WITHOUT</span> <span style="color: #993333; font-weight: bold;">TIME</span> zone <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> now<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>                                    
 published <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">TIMESTAMP</span> <span style="color: #993333; font-weight: bold;">WITHOUT</span> <span style="color: #993333; font-weight: bold;">TIME</span> zone <span style="color: #66cc66;">|</span>                                                  
 author    <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">INTEGER</span>                     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>                                         
Indexes:                                                                                    
    <span style="color: #ff0000;">&quot;posts_primary_key_id&quot;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span> btree <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>                                          
Foreign<span style="color: #66cc66;">-</span><span style="color: #993333; font-weight: bold;">KEY</span> constraints:                                                                    
    <span style="color: #ff0000;">&quot;posts_fkey_author&quot;</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;author&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> authors<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span></pre></div></div>

<div class="asideBlock">Every time you type <em>(rollback)</em> Lobos will try to rollback the schema down one migration using the <em>down</em> function. running <em>(migrate)</em> again will bring the schema back to latest version.</div>
<p>Lobos keeps track of which migrations are run using the lobos_migrations table. As you can see below our current lobos_migrations contains two migrations we have written and executed.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">clogdb<span style="color: #66cc66;">=</span># <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> lobos_migrations;  
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> lobos_migrations;           
      name                                
<span style="color: #808080; font-style: italic;">-----------------                         </span>
 add<span style="color: #66cc66;">-</span>authors<span style="color: #66cc66;">-</span><span style="color: #993333; font-weight: bold;">TABLE</span>                          
 add<span style="color: #66cc66;">-</span>posts<span style="color: #66cc66;">-</span><span style="color: #993333; font-weight: bold;">TABLE</span>                          
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">ROWS</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>Entity Definition</h3>
<p>After creating the database, now we&#8217;ll define our entities using the SQL library <a href="http://sqlkorma.com">Korma</a>. What is Korma ?</p>
<blockquote><p>Korma is a domain specific language for Clojure that takes the pain out of working with your favorite RDBMS. Built for speed and designed for flexibility, Korma provides a simple and intuitive interface to your data that won&#8217;t leave a bad taste in your mouth.</p></blockquote>
<p>Korma makes it easier to deal with the database. Instead of using SQL queries, you can use Clojure to access the database. To get the data from the tables, first we need to define entities using the Korma&#8217;s <em>defnentity</em> macro. First create a file called models.clj in your src/clog folder. And add the following code to the file:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> clog<span style="color: #66cc66;">.</span>models
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">use</span> korma<span style="color: #66cc66;">.</span>db
        korma<span style="color: #66cc66;">.</span>core<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defdb clogdb
  <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">classname</span> <span style="color: #ff0000;">&quot;org.postgresql.Driver&quot;</span>
   :<span style="color: #555;">subprotocol</span> <span style="color: #ff0000;">&quot;postgresql&quot;</span>
   :<span style="color: #555;">subname</span> <span style="color: #ff0000;">&quot;clogdb&quot;</span>
   :<span style="color: #555;">user</span> <span style="color: #ff0000;">&quot;clog&quot;</span>
   :<span style="color: #555;">password</span> <span style="color: #ff0000;">&quot;clog&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defentity authors<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defentity posts<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The above code defines two entities <em>authors</em> and <em>posts</em>. Let me show you how we can insert an author record into the database and select the list of authors from the database. Start Clojure REPL, if it isn&#8217;t running already. Type the following code into REPL:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>use 'korma<span style="color: #66cc66;">.</span>db 'korma<span style="color: #66cc66;">.</span>core 'clog<span style="color: #66cc66;">.</span>models<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>insert  authors <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">id</span> <span style="color: #cc66cc;">1</span>, :<span style="color: #555;">username</span> <span style="color: #ff0000;">&quot;vijay&quot;</span>, :<span style="color: #555;">password</span> <span style="color: #ff0000;">&quot;password&quot;</span>, :<span style="color: #555;">email</span> <span style="color: #ff0000;">&quot;mail AT vijaykiran.com&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This will insert a new record into the database. You can use the <em>select</em> function to get the list of authors from the authors table.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>select authors<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">id</span> <span style="color: #cc66cc;">1</span>, :<span style="color: #555;">username</span> <span style="color: #ff0000;">&quot;vijay&quot;</span>, :<span style="color: #555;">password</span> <span style="color: #ff0000;">&quot;password&quot;</span>, :<span style="color: #555;">email</span> <span style="color: #ff0000;">&quot;mail AT vijaykiran.com&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<h3>Source Code</h3>
<p>If you are stuck at any point and want to directly checkout the code, you can use the following commands to get the code that is tagged with part2</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">git clone git://github.com/vijaykiran/clog.git
cd clog
git checkout part2</pre></div></div>

<h2>Conclusion</h2>
<p>This concludes the part 2 of this series. In this part of the tutorial we saw how to create a simple schema using Lobos library, also we defined the entities for the tables we created using the Korma library. In the next part I&#8217;ll show you how to populate sample data into the database and we&#8217;ll create our first template to display the data in the browser.</p>
<p>Make sure you <a href="vijaykiran.com/feed/">subscribe</a> to the RSS feed or follow me on <a href="http://twitter.com/vijaykiran">Twitter</a> to get notified.</p>
<p>If you have any feedback on this article and its content, you can contact me via <a href="http://twitter.com/vijaykiran">Twitter</a>.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/Vq0-8RbxXlVXTgL87DQxxZ1mwkU/0/da"><img src="http://feedads.g.doubleclick.net/~a/Vq0-8RbxXlVXTgL87DQxxZ1mwkU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Vq0-8RbxXlVXTgL87DQxxZ1mwkU/1/da"><img src="http://feedads.g.doubleclick.net/~a/Vq0-8RbxXlVXTgL87DQxxZ1mwkU/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=PSYWiOnEVgU:SLFjO23fQfs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=PSYWiOnEVgU:SLFjO23fQfs:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=PSYWiOnEVgU:SLFjO23fQfs:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/PSYWiOnEVgU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/</feedburner:origLink></item>
		<item>
		<title>→ The Rise of the New Groupthink</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/gmG42_vPcNE/</link>
		<comments>http://www.vijaykiran.com/2012/01/15/the-rise-of-the-new-groupthink/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 08:44:19 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Links]]></category>
		<category><![CDATA[groupthink]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[workplace design]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3649</guid>
		<description><![CDATA[To harness the energy that fuels both these drives, we need to move beyond the New Groupthink and embrace a more nuanced approach to creativity and learning. Our offices should encourage casual, cafe-style interactions, but allow people to disappear into personalized, private spaces when they want to be alone. Our schools should teach children to [...]]]></description>
			<content:encoded><![CDATA[<p>To harness the energy that fuels both these drives, we need to move beyond the New Groupthink and embrace a more nuanced approach to creativity and learning. Our offices should encourage casual, cafe-style interactions, but allow people to disappear into personalized, private spaces when they want to be alone. Our schools should teach children to work with others, but also to work on their own for sustained periods of time. And we must recognize that introverts like Steve Wozniak need extra quiet and privacy to do their best work.</p>
<p>via <a href='http://www.nytimes.com/2012/01/15/opinion/sunday/the-rise-of-the-new-groupthink.html?_r=1&#038;pagewanted=all'>The Rise of the New Groupthink &#8211; NYTimes.com</a>.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/w6ZdMhWJBi3N9Z52UHDVirmciNY/0/da"><img src="http://feedads.g.doubleclick.net/~a/w6ZdMhWJBi3N9Z52UHDVirmciNY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/w6ZdMhWJBi3N9Z52UHDVirmciNY/1/da"><img src="http://feedads.g.doubleclick.net/~a/w6ZdMhWJBi3N9Z52UHDVirmciNY/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=gmG42_vPcNE:drPFYxlNDDs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=gmG42_vPcNE:drPFYxlNDDs:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=gmG42_vPcNE:drPFYxlNDDs:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/gmG42_vPcNE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/01/15/the-rise-of-the-new-groupthink/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/01/15/the-rise-of-the-new-groupthink/</feedburner:origLink></item>
		<item>
		<title>Photo: Veggies</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/5ea5ojER61I/</link>
		<comments>http://www.vijaykiran.com/2012/01/12/photo-veggies/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 08:05:08 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3642</guid>
		<description />
			<content:encoded><![CDATA[<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/01/20120112-090225.jpg"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/01/20120112-090225.jpg" alt="20120112-090225.jpg" class="alignnone size-full" /></a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/ffyIVH_pXcI6QLFY1BXbA1-An1Q/0/da"><img src="http://feedads.g.doubleclick.net/~a/ffyIVH_pXcI6QLFY1BXbA1-An1Q/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/ffyIVH_pXcI6QLFY1BXbA1-An1Q/1/da"><img src="http://feedads.g.doubleclick.net/~a/ffyIVH_pXcI6QLFY1BXbA1-An1Q/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=5ea5ojER61I:nbtd0uvb6Qk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=5ea5ojER61I:nbtd0uvb6Qk:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=5ea5ojER61I:nbtd0uvb6Qk:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/5ea5ojER61I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/01/12/photo-veggies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/01/12/photo-veggies/</feedburner:origLink></item>
		<item>
		<title>Web Application Development with Clojure – Part 1</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/_NDiFoSMULY/</link>
		<comments>http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 16:48:28 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[intellij]]></category>
		<category><![CDATA[lobos]]></category>
		<category><![CDATA[webapp]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3548</guid>
		<description><![CDATA[This post is part of the Web Application Development with Clojure tutorial. Part 1: Project Setup Part 2: Data model definition with Lobos and Korma Part 3: Loading Fixures with clj-yaml and HTML Templating with Enlive Part 4: Adding CSS, Post detail page and Simple Authentication Part 5: Blog Administration Area Introduction Clojure is general-purpose [...]]]></description>
			<content:encoded><![CDATA[<p>This post is part of the Web Application Development with <a href="http://clojure.org">Clojure</a> tutorial.</p>
<ul>
<li><a href="http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/">Part 1: Project Setup</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/">Part 2: Data model definition with Lobos and Korma</a></li>
<li><a href="http://www.vijaykiran.com/2012/01/31/web-application-development-with-clojure-part-3/">Part 3: Loading Fixures with <a href="https://github.com/lancepantz/clj-yaml">clj-yaml</a> and HTML Templating with <a href="https://github.com/cgrand/enlive">Enlive</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/12/web-application-development-with-clojure-part-4/">Part 4: Adding CSS, Post detail page and Simple Authentication</a></li>
<li><a href="http://www.vijaykiran.com/2012/02/27/web-application-development-with-clojure-part-5/">Part 5: Blog Administration Area</a></li>
</ul>
<h2>Introduction</h2>
<p><a href="http://clojure.org">Clojure</a> is general-purpose compiled, dynamic, Lisp-dialect language that targets JVM (also .Net CLR and JavaScript). There are many ways you can build a Web application using Clojure. You can use a simple framework like <a href="http://webnoir.org">Noir</a> or you can pick and choose libraries you like and build your own web stack along the way. I assume that you already started learning Clojure using some tutorial or a good Clojure book.</p>
<p>In this blog post series I&#8217;ll explain how to build a web application using Clojure. Creating a blogging engine seems to be the equivalent &#8220;hello world&#8221; for Web application development, so let us build a blog engine in Clojure: Clog.</p>
<h2>Clojure Web Application Toolkit</h2>
<p>There are many fantastic libraries available for Clojure for any type of task you can imagine and superior Java Interoperability of <a href="http://clojure.org">Clojure</a> gives you seamless access to innumerable Java libraries and API that you can benefit from. To build a database backed web application you might need some standard libraries to perform HTTP Routing, Database connectivity and migrations, HTML Templates etc.</p>
<p>We&#8217;ll use the following libraries to build our web application:</p>
<ul>
<li><em><a href="http://clojure.org">Clojure</a></em> &#8211; <em>1.3.0</em> &#8211; The language.</li>
<li><em><a href="https://github.com/mmcgrana/ring">ring</a></em> &#8211; &nbsp;<em>1.0.1</em> &#8211; The HTTP Abstraction API and Middleware.</li>
<li><em><a href="https://github.com/cgrand/moustache">moustache</a></em> &nbsp;- <em>1.1.0</em> &#8211; HTTP Routes DSL</li>
<li><em><a href="http://budu.github.com/lobos/">lobos</a></em> &#8211; &nbsp;<em>1.0.0-SNAPSHOT</em> &#8211; Database Schema Manipulation and Migrations</li>
<li><em><a href="http://sqlkorma.com/">korma</a></em> &#8211; <em>0.2.1</em> &#8211; DSL for Database Connectivity and Interaction.</li>
<li><em><a href="https://github.com/cgrand/enlive">enlive</a></em> &#8211; <em>1.0.0</em> &#8211; Selector based HTML library.</li>
<li><em>postgresql</em> -&nbsp;<em>9.1-901.jdbc4</em> &#8211; PostgreSQL JDBC4 driver.</li>
<li><em><a href="https://github.com/lancepantz/clj-yaml">clj-yaml</a></em> &#8211; <em>0.3.1</em> &#8211; A snakeyaml wrapper for YAML file parsing and generation.</li>
</ul>
<div class="asideBlock">Apache Maven is more than just a &#8220;dependency management&#8221; tool. It is a &#8220;software project management and comprehension tool&#8221;. Don&#8217;t worry too much if you don&#8217;t <em>comprehend</em> what it means.</div>
<p>The above list is just for your information. You don&#8217;t need to individually download those libraries. For those of you who are familiar with Java dependency management tools like <a href="http://maven.apache.org/">Apache Maven</a> or <a href="http://ant.apache.org/ivy/">Apache Ivy</a>, Clojure ecosystem has similar build tool called&nbsp;<a href="https://github.com/technomancy/leiningen/">Leineingen</a>&nbsp;which helps you configure your project, manage dependencies etc.</p>
<blockquote><p>Leiningen is for automating Clojure projects without setting your hair on fire.Working on Clojure projects with tools designed for Java can be an exercise in frustration. With Leiningen, you just write Clojure.</p></blockquote>
<p>Using Leiningen you can configure your project settings using Clojure. Leiningen uses Apache Maven for dependency resolution. So you can add any library from a maven repository as a dependency to your Clojure project. Installing Leiningen is very straight forward, which is left as an exercise to the reader.</p>
<h2>Project Setup</h2>
<p>After you have installed leiningen you can create a project by typing the following command in your terminal (or command) window.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">lein new clog</pre></div></div>

<p>If leiningen is installed properly you should see an output of the command similar to the following</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">→ ~$ lein new clog
Created new project in: /Users/vijaykiran/clog
Look over project.clj and start coding in clog/core.clj</pre></div></div>

<p>When you run the above command, leiningen will create a simple Clojure project named &#8220;clog&#8221; for you. If you look into the clog folder, you see there are several files and folders created. Again, if you are familiar with Apache Maven, you can see the structure is pretty similar to standard Maven project. Anyway, let us take a quick look at the generated files and folders:</p>
<ul>
<li><em>README</em> &#8211; A simple README file</li>
<li><em>project.clj</em> &#8211; The Clojure project configuration file &#8211; written in Clojure.</li>
<li><em>src</em> &#8211; Folder for your source code</li>
<li><em>test</em> &#8211; Folder for your tests</li>
<li><em>src/clog/core.clj</em> &#8211; The &#8220;core&#8221; Clojure file for your project (You can change it to whatever you want, but for simplicity, we&#8217;ll leave it as it is).</li>
</ul>
<p>Create a directory called <em>resources</em> inside the clog directory. This directory will be used to store and serve the static resources like images, CSS files etc. of our web application.</p>
<p>There are many configuration options available to set up your project via the <em>project.clj</em> file. But for now we&#8217;ll just focus on simple things like adding dependencies.</p>
<h3>Adding Dependencies</h3>
<p>If you open the <strong><em>project.clj</em></strong> file under your clog folder, you can already see that there are a couple of dependencies configured using <em>defproject</em> macro.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defproject clog <span style="color: #ff0000;">&quot;1.0.0-SNAPSHOT&quot;</span>
  :<span style="color: #555;">description</span> <span style="color: #ff0000;">&quot;FIXME: write description&quot;</span>
  :<span style="color: #555;">dependencies</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>org<span style="color: #66cc66;">.</span>clojure<span style="color: #66cc66;">/</span>clojure <span style="color: #ff0000;">&quot;1.3.0&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Let us update the file to add the required libraries (as shown above). Use your favorite editor to edit the <em>project.clj</em>, we&#8217;ll later setup an IDE for editing our source code. While we are there, we&#8217;ll also fix the version and description of the project and set them to <em>0.0.1-SNAPSHOT</em> and <em>A Clojure Blogging Engine</em>.</p>
<div class="asideBlock">This would be good time to create a version control repository and add your source code to it. If you are using git, you can see that <em>lein new</em> has already created a .gitignore file for you. So all you need to do is <em>git init</em>, <em>git add .</em> and <em>git commit -m &#8220;Clog project &#8211; init&#8221;</em> to create and initialize the git repository for your project.</div>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defproject clog <span style="color: #ff0000;">&quot;0.0.1-SNAPSHOT&quot;</span>
  :<span style="color: #555;">description</span> <span style="color: #ff0000;">&quot;Clog: A Blog engine written in Clojure&quot;</span>
  :<span style="color: #555;">dependencies</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>org<span style="color: #66cc66;">.</span>clojure<span style="color: #66cc66;">/</span>clojure <span style="color: #ff0000;">&quot;1.3.0&quot;</span><span style="color: #66cc66;">&#93;</span>
                 <span style="color: #66cc66;">&#91;</span>ring <span style="color: #ff0000;">&quot;1.0.1&quot;</span> <span style="color: #808080; font-style: italic;">;;; Exclude the clojure, clj-stacktrace from ring dependency</span>
                   :<span style="color: #555;">exclusions</span> <span style="color: #66cc66;">&#91;</span>org<span style="color: #66cc66;">.</span>clojure<span style="color: #66cc66;">/</span>clojure
                               clj<span style="color: #66cc66;">-</span>stacktrace<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
                 <span style="color: #66cc66;">&#91;</span>net<span style="color: #66cc66;">.</span>cgrand<span style="color: #66cc66;">/</span>moustache <span style="color: #ff0000;">&quot;1.1.0&quot;</span><span style="color: #66cc66;">&#93;</span>
                 <span style="color: #66cc66;">&#91;</span>lobos <span style="color: #ff0000;">&quot;1.0.0-SNAPSHOT&quot;</span><span style="color: #66cc66;">&#93;</span>
                 <span style="color: #66cc66;">&#91;</span>korma <span style="color: #ff0000;">&quot;0.2.1&quot;</span><span style="color: #66cc66;">&#93;</span>
                 <span style="color: #66cc66;">&#91;</span>enlive <span style="color: #ff0000;">&quot;1.0.0&quot;</span><span style="color: #66cc66;">&#93;</span>
                 <span style="color: #66cc66;">&#91;</span>postgresql <span style="color: #ff0000;">&quot;9.1-901.jdbc4&quot;</span><span style="color: #66cc66;">&#93;</span>
                 <span style="color: #66cc66;">&#91;</span>clj<span style="color: #66cc66;">-</span>yaml <span style="color: #ff0000;">&quot;0.3.1&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>After you edit the <em>project.clj</em>, run the <em>lein deps</em> command to download the required dependencies. You should see output similar to this:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">~/clog $ lein deps
...
Copying 32 files to /Users/vijaykiran/clog/lib</pre></div></div>

<p>That is all you need to do to configure and setup your project. Of course, we still need to set up the database, which we will come back to at a later stage. Next step is configure a development environment a.k.a an IDE.</p>
<h2>IDE Configuration</h2>
<p>All major IDEs (Eclipse with Couter-ClockWise plugin, IntelliJ IDEA with La Clojure plugin and Netbeans with Enclojure plugin) have Clojure support via Plugins. Many Clojure developers seem to prefer Emacs instead of using IDEs.</p>
<h3>Emacs with SLIME</h3>
<p>Emacs seems to be the favorite editor of most people in Clojure community. Emacs has several &#8220;plugins&#8221; that help with editing Clojure files. Learning &amp; using Emacs can be intimidating at first but personally I find it very rewarding experience. If you are an Emacs user you can use clojure-mode, slime, swank and paredit as your development environment. There are <a href="http://stackoverflow.com/questions/2285437/a-gentle-tutorial-to-emacs-swank-paredit-for-clojure">several</a> <a href="http://data-sorcery.org/2009/12/20/getting-started/">tutorials</a> available on how to configure Emacs for clojure development. I encourage you to check them out if you want to use Emacs as your &#8220;IDE&#8221;. I use Emacs with lein swank, clojure-mode and slime-connect. I configured emacs with emacs starter kit and other plugins, FWIW, this is how it looks:</p>
<div class="asideBlock">You can see project.clj in editor mode, Clojure REPL using slime-connection and lein swank running in Emacs&#8217;s eshell on this image. Since my Emacs configuration is based on <a href="https://github.com/overtone/live-coding-emacs">Live-Coding Emacs</a> setup, I get a nice color scheme and auto-completion.</div>
<p><a class="enlarge" href="http://www.vijaykiran.com/wp-content/uploads/2012/01/emacs-slime.png"><img class="aligncenter size-large wp-image-3601" title="emacs-slime" src="http://www.vijaykiran.com/wp-content/uploads/2012/01/emacs-slime-620x405.png" alt="" width="620" height="405" /></a></p>
<h3>IntelliJ IDEA</h3>
<p>For this tutorial I will use <a href="http://www.jetbrains.com/idea/">IntelliJ Community Edition</a> with <a href="http://plugins.intellij.net/plugin/?id=4050">La Clojure</a> and <a href="http://plugins.intellij.net/plugin/?id=5029">Leiningen</a> Plugins. After downloading the IDE make sure you install and enable the plugins.</p>
<p><a class="enlarge" href="http://www.vijaykiran.com/wp-content/uploads/2012/01/IntelliJ-Plugins.png"><img class="aligncenter size-large wp-image-3591" title="IntelliJ-Plugins" src="http://www.vijaykiran.com/wp-content/uploads/2012/01/IntelliJ-Plugins-620x521.png" alt="" width="620" height="521" /></a></p>
<p>After you have installed both plugins, select <em>Open Project ..</em> option in IntelliJ and choose the <em>project.clj</em> from <em>clog</em> folder. Leiningen plugin will detect the project.clj and open the clog project as an IntelliJ project as shown below.</p>
<p><a class="enlarge" href="http://www.vijaykiran.com/wp-content/uploads/2012/01/idea-clog-project.png"><img class="aligncenter size-large wp-image-3606" title="idea-clog-project" src="http://www.vijaykiran.com/wp-content/uploads/2012/01/idea-clog-project-620x455.png" alt="" width="620" height="455" /></a></p>
<p>Make sure that you add Clojure facet to the module, which will allow you start the Clojure console for the project. Also, add the clojure 1.3 library for project if you have another version of clojure installed globally. This will make sure that when you start the Clojure console, it will use the correct Clojure version.</p>
<h2>Startup code for Jetty Server</h2>
<p>First step in developing a web application is to startup the jetty server. We will use the ring-jetty adapter to start-up the Jetty server. Edit the <em>core.clj</em> file and add the following code to it:</p>
<div class="asideBlock">When you import other namespaces using <em>:use</em> you are actually importing all the symbols into current namespace. This might create conflicts with the symbols you define in your namespace, so it is advised that you use <em>:require</em> instead. To keep things very simple, we use <em>:use</em> here.</div>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> clog<span style="color: #66cc66;">.</span>core
  <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">use</span> ring<span style="color: #66cc66;">.</span>adapter<span style="color: #66cc66;">.</span>jetty
        ring<span style="color: #66cc66;">.</span>middleware<span style="color: #66cc66;">.</span>resource
        ring<span style="color: #66cc66;">.</span>util<span style="color: #66cc66;">.</span>response
        net<span style="color: #66cc66;">.</span>cgrand<span style="color: #66cc66;">.</span>moustache<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;;; A simple handler to show send some response to the client.</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> index
  <span style="color: #66cc66;">&#91;</span>req<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>response <span style="color: #ff0000;">&quot;Welcome, to Clog - A Blog Engine written in Clojure&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Routes definition</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> routes
  <span style="color: #66cc66;">&#40;</span>app
    <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#93;</span> index<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;;; start function for starting jetty</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> start <span style="color: #66cc66;">&#91;</span>port<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>run<span style="color: #66cc66;">-</span>jetty #'routes <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">port</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> port <span style="color: #cc66cc;">8080</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #555;">join</span>? false<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Let us take a closer look on the code we added to the core.clj. First we defined the namespace <em>clog.core</em> and imported the namespaces via <em>:use</em>. Then we defined a simple function called <em>index</em> that uses the ring <em>response</em> method to send some text as response.</p>
<p>We used the moustache macro <em>app</em> to define the routes that we want to handle. In our case, we want the route that matches the url segment with parts [""] to the function <em>index</em>, which is another way saying when a client sends a request with no url segments or parameters, let the function <em>index</em> handle it.</p>
<p>Next we defined a function <em>start</em> that will help us to start a jetty server. It takes a port parameter, if it isn&#8217;t supplied, the run-jetty function from ring-jetty adapter will start the jetty server on port 8080.</p>
<div class="asideBlock">REPL is an acronym for <strong>R</strong>ead <strong>E</strong>val <strong>P</strong>rint <strong>L</strong>oop, which does what the name implies &#8211; it <em>reads</em> the code, <em>evaluates</em> it, <em>prints</em> the result and <em>loops</em> back to read step. The main difference between Clojure REPL and other languages&#8217; is during the Eval step, the code is compiled.</div>
<h3>Running the program in lein REPL</h3>
<p>You can run this program using REPL in command line. Open a terminal window, and change to the clog directory. Then type <em>lein repl</em>, which will start a new Clojure REPL with the class path setup to include our project&#8217;s libraries. In the REPL, first import the <em>clog.core</em> namespace by typing <em>use &#8216;clog.core</em>. Now you can use the <em>start</em> function to start jetty on your port of choice. Here&#8217;s the transcript of the commands and their results:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">→ ~$ cd clog
→ clog[master]/$ lein repl
REPL started; server listening on localhost port 53791
user=&amp;gt; (use 'clog.core)
nil
user=&amp;gt; (start 8888)
2012-01-11 16:10:41.640:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
#
2012-01-11 16:10:41.642:INFO::jetty-6.1.25
2012-01-11 16:10:41.654:INFO::Started SocketConnector@0.0.0.0:8888
user=&amp;gt;</pre></div></div>

<p>Open a browser window and open the URL <em>http://localhost:8888</em>. You should see the message <em>Welcome, to Clog &#8211; A Blog Engine written in Clojure</em>.</p>
<h3>Running the program in IntelliJ IDE</h3>
<p>To run this program in IntelliJ, start the Clojure console by clicking on the Tools -&gt; Start Clojure Console. This will start a console in your IDE with the your module&#8217;s class path. Now type <em>(use &#8216;clog.core)</em> and <em>(start 8888)</em> in your clojure console. You should see the output as shown below:</p>
<p><a class="enlarge" href="http://www.vijaykiran.com/wp-content/uploads/2012/01/running-clog-in-ide-repl.png"><img class="alignleft size-large wp-image-3623" title="running-clog-in-ide-repl" src="http://www.vijaykiran.com/wp-content/uploads/2012/01/running-clog-in-ide-repl-620x278.png" alt="" width="620" height="278" /></a></p>
<p>You can now browse to the URL: http://localhost:8888 to see the response.</p>
<h2>Conclusion</h2>
<p>This concludes the part 1 of this series. In part 2 I&#8217;ll show you how to create the database schema and migrations using Clojure and <a href="http://budu.github.com/lobos/">lobos</a>. Make sure you <a href="http://vijaykiran.com/feed/">subscribe</a> to the RSS feed or follow me on <a href="http://twitter.com/vijaykiran">Twitter</a> to get notified.</p>
<p>If you have any feedback on this article and its content, you can contact me via <a href="http://twitter.com/vijaykiran">Twitter</a>.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/AdRmHBQdu6FeOYXcVIuPOPeFYNE/0/da"><img src="http://feedads.g.doubleclick.net/~a/AdRmHBQdu6FeOYXcVIuPOPeFYNE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/AdRmHBQdu6FeOYXcVIuPOPeFYNE/1/da"><img src="http://feedads.g.doubleclick.net/~a/AdRmHBQdu6FeOYXcVIuPOPeFYNE/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=_NDiFoSMULY:PYQ-pgggUtg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=_NDiFoSMULY:PYQ-pgggUtg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=_NDiFoSMULY:PYQ-pgggUtg:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/_NDiFoSMULY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/01/11/web-application-development-with-clojure-part-1/</feedburner:origLink></item>
		<item>
		<title>The Making of “Chana Methi Matki Biryani”</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/Zr6Wyt5BTj0/</link>
		<comments>http://www.vijaykiran.com/2012/01/08/matka-chana-methi-matkibiryani/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 19:42:42 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Cooking]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[biryani]]></category>
		<category><![CDATA[food]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3508</guid>
		<description><![CDATA[Sunday Dinner today is Chana Methi Matki Biryani. First an emergency Glossary so that the title makes some sense. Chana : Chickpeas Methi :Fenugreek Leaves Matki : Earthen Pot Basmati Rice: Basmati Rice The main ingredients Tomatoes &#8211; Deseeded, diced Red onion Methi Leaves Mint leaves Chana &#8211; soaked overnight and boiled Saffron &#8211; soaked [...]]]></description>
			<content:encoded><![CDATA[<p>Sunday Dinner today is <strong>Chana Methi Matki Biryani</strong>. First an emergency Glossary so that the title makes some sense.</p>
<ul>
<li>Chana : <a href="http://en.wikipedia.org/wiki/Chickpea">Chickpeas</a></li>
<li>Methi :<a href="http://en.wikipedia.org/wiki/Methi">Fenugreek Leaves</a></li>
<li>Matki : <a href="http://en.wikipedia.org/wiki/Matki_(earthen_pot)"> Earthen Pot</a></li>
<li>Basmati Rice: <a href="http://en.wikipedia.org/wiki/Basmati_rice">Basmati Rice</a></li>
</ul>
<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/01/biryani-ingredients.jpg"><img class="aligncenter  wp-image-3511" title="biryani-ingredients" src="http://www.vijaykiran.com/wp-content/uploads/2012/01/biryani-ingredients-620x463.jpg" alt="Biryani Ingredients" width="558" height="417" /></a><br />
<!-- more --><br />
<strong>The main ingredients</strong></p>
<ul>
<li>Tomatoes &#8211; Deseeded, diced</li>
<li>Red onion</li>
<li>Methi Leaves</li>
<li>Mint leaves</li>
<li>Chana &#8211; soaked overnight and boiled</li>
<li>Saffron &#8211; soaked in warm water</li>
<li>Briyani Rice &#8211; washed and soaked in warm water</li>
<li>Yogurt</li>
</ul>
<p><strong>Spices:</strong></p>
<ul>
<li>Cloves</li>
<li>Bay leaves</li>
<li>Black Cardamom</li>
<li>Cinnamon Sticks</li>
<li>Green Cardamom</li>
<li>Black Cumin Seeds</li>
<li>Raisins</li>
<li>Green Chili &#8211; Sliced vertically</li>
<li>Star Anise</li>
<li>Chopped Ginger and Garlic</li>
</ul>
<p><strong>Making the Chana &#8220;Masala&#8221;</strong><br />
Heat some ghee, add the spices (Cloves, Bay leaves, Black and green cardamom, cumin seeds and star anise). After a couple of minutes add chopped onions, chili, chopped ginger and garlic. Sauté for a 3-4 minutes. Add Chopped tomatoes. Once onion and tomato become soft add Chickpeas and Methi leaves. Keep stirring and keep cooking on medium heat.</p>
<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/01/chana.jpg"><img class="aligncenter  wp-image-3522" title="chana" src="http://www.vijaykiran.com/wp-content/uploads/2012/01/chana-620x620.jpg" alt="" width="496" height="496" /></a></p>
<p>After 4-5 minutes of cooking add yogurt and mix thoroughly. Reduce the heat and cook for a couple of more minutes. You don&#8217;t need to cook it all the way, since it will be put into the oven in a Matki.</p>
<p>Spread the chana masala as a thick layer in a Matki (Earthen Pot), and spread soaked rice on top of it as a second layer. On the rice sprinkle saffron water, a pinch of gram masala and some mint leaves. Add another layer of Chana Masala and Rice continue till you fill up the Matki.</p>
<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/01/biryani-rice-layers1.jpg"><img class="alignleft  wp-image-3527" title="biryani-rice-layers1" src="http://www.vijaykiran.com/wp-content/uploads/2012/01/biryani-rice-layers1-328x440.jpg" alt="" width="276" height="370" /></a><a href="http://www.vijaykiran.com/wp-content/uploads/2012/01/biryani-rice-layers2.jpg"><img class="alignleft  wp-image-3526" title="biryani-rice-layers2" src="http://www.vijaykiran.com/wp-content/uploads/2012/01/biryani-rice-layers2-328x440.jpg" alt="" width="276" height="370" /></a></p>
<p>Place the pot in the oven pre-heated to 200° Celsius &#8211; cook for 1.5 hours. Enjoy with Raita.</p>
<p><a href="http://www.vijaykiran.com/wp-content/uploads/2012/01/final.jpg"><img src="http://www.vijaykiran.com/wp-content/uploads/2012/01/final-620x463.jpg" alt="" title="final" width="620" height="463" class="aligncenter size-large wp-image-3543" /></a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/XCfmDIjkmNOn_E_SxB-a15r-rMU/0/da"><img src="http://feedads.g.doubleclick.net/~a/XCfmDIjkmNOn_E_SxB-a15r-rMU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/XCfmDIjkmNOn_E_SxB-a15r-rMU/1/da"><img src="http://feedads.g.doubleclick.net/~a/XCfmDIjkmNOn_E_SxB-a15r-rMU/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=Zr6Wyt5BTj0:e5MqDHeq-HU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=Zr6Wyt5BTj0:e5MqDHeq-HU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=Zr6Wyt5BTj0:e5MqDHeq-HU:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/Zr6Wyt5BTj0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2012/01/08/matka-chana-methi-matkibiryani/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2012/01/08/matka-chana-methi-matkibiryani/</feedburner:origLink></item>
		<item>
		<title>Jason Fried on Nokia Lumia 800 + WP7.5</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/cjPIzexaAag/</link>
		<comments>http://www.vijaykiran.com/2011/12/28/jason-fried-on-nokia-lumia-800-wp7-5/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 19:29:04 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Links]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[nokia lumia]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3481</guid>
		<description><![CDATA[I’ve been hugely impressed with the Nokia Lumia 800 running Windows Phone 7.5. I’ve had an iPhone since day one, and every model since, but I really like the new path Microsoft is cutting with WP7 so I decided to give it an extended try. It’s been a bit over a week, and so far [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>I’ve been hugely impressed with the Nokia Lumia 800 running Windows Phone 7.5. I’ve had an iPhone since day one, and every model since, but I really like the new path Microsoft is cutting with WP7 so I decided to give it an extended try. It’s been a bit over a week, and so far I don’t miss my iPhone at all. In fact, going back to the iPhone feels like going back in time. It’s rough around the edges in a few spots, but I think Microsoft is up to something good here. More on this in a future post after I’ve spent some more quality time with the phone/platform.</p></blockquote>
<p>via <a href="http://37signals.com/svn/posts/3050-some-great-stuff-ive-tried-recently">Some great stuff Ive tried recently &#8211; 37signals</a>.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/Ryjn9j0EoQHK-zTfauLWi_SeQ1k/0/da"><img src="http://feedads.g.doubleclick.net/~a/Ryjn9j0EoQHK-zTfauLWi_SeQ1k/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Ryjn9j0EoQHK-zTfauLWi_SeQ1k/1/da"><img src="http://feedads.g.doubleclick.net/~a/Ryjn9j0EoQHK-zTfauLWi_SeQ1k/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=cjPIzexaAag:Vjon3-0v7WE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=cjPIzexaAag:Vjon3-0v7WE:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=cjPIzexaAag:Vjon3-0v7WE:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/cjPIzexaAag" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2011/12/28/jason-fried-on-nokia-lumia-800-wp7-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2011/12/28/jason-fried-on-nokia-lumia-800-wp7-5/</feedburner:origLink></item>
		<item>
		<title>Difference between 0 and ‘nothing’</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/IJAZ46c1CH8/</link>
		<comments>http://www.vijaykiran.com/2011/12/28/difference-between-0-and-nothing/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 17:42:12 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[0]]></category>
		<category><![CDATA[maths]]></category>
		<category><![CDATA[nothing]]></category>
		<category><![CDATA[quotes]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3478</guid>
		<description><![CDATA[0 vs. nothing is one of those abstract distinctions that&#8217;s almost impossible to talk about directly; you more have to do it with examples. Imagine there&#8217;s a certain math class, and in this class there&#8217;s a fiendishly difficult 100-point midterm, and imagine that neither you nor I get even one point out of 100 on [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>0 vs. nothing is one of those abstract distinctions that&#8217;s almost impossible to talk about directly; you more have to do it with examples. Imagine there&#8217;s a certain math class, and in this class there&#8217;s a fiendishly difficult 100-point midterm, and imagine that neither you nor I get even one point out of 100 on this exam. Except there&#8217;s a difference: you are not in the class and didn&#8217;t even take the exam, where as I am, and did. The fact that you received 0 points on the exam is thus irrelevant — your 0 means N/A, nothing — where are my 0 is an actual zero. Or if you don&#8217;t like that one, imagine that you and I are respectively female and male, both healthy and 20—40 years of age, and we&#8217;re both at the doctor&#8217;s, and neither of us has had a menstrual period in the past ten weeks, in which case my total number of periods is nothing, whereas yours here is 0 — and significant.</p></blockquote>
<p>- From <a href="http://www.amazon.com/gp/product/0393003388/ref=as_li_ss_tl?ie=UTF8&amp;tag=vijaykicom-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0393003388">Everything and More: A Compact History of Infinity (Great Discoveries)</a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=vijaykicom-20&amp;l=as2&amp;o=1&amp;a=0393003388" alt="" width="1" height="1" border="0" /></p>

<p><a href="http://feedads.g.doubleclick.net/~a/INzKvHkjFlMfC_EO05FJwh2eGLE/0/da"><img src="http://feedads.g.doubleclick.net/~a/INzKvHkjFlMfC_EO05FJwh2eGLE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/INzKvHkjFlMfC_EO05FJwh2eGLE/1/da"><img src="http://feedads.g.doubleclick.net/~a/INzKvHkjFlMfC_EO05FJwh2eGLE/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=IJAZ46c1CH8:f7tCkL_DMNo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=IJAZ46c1CH8:f7tCkL_DMNo:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=IJAZ46c1CH8:f7tCkL_DMNo:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/IJAZ46c1CH8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2011/12/28/difference-between-0-and-nothing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2011/12/28/difference-between-0-and-nothing/</feedburner:origLink></item>
		<item>
		<title>Abstraction Levels in Mathematics</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/s-vEjAs3JFY/</link>
		<comments>http://www.vijaykiran.com/2011/11/29/abstraction-levels-in-mathematics/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 19:21:10 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[david foster wallace]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[∞]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3472</guid>
		<description><![CDATA[&#8230; consider for a moment how many levels of abstraction are involved in math itself. In arithmetic there&#8217;s the abstraction of number; and then there&#8217;s algebra, with a variable being a further-abstracted symbol for some number(s) and a function being a precise but abstract relation between domains of variables; and then of course there&#8217;s college [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>
&#8230; consider for a moment how many levels of abstraction are involved in math itself. In arithmetic there&#8217;s the abstraction of number; and then there&#8217;s algebra, with a variable being a further-abstracted symbol for some number(s) and a function being a precise but abstract relation between domains of variables; and then of course there&#8217;s college math&#8217;s derivatives and integrals of functions, and then integral equations involving unknown functions, and differential equations&#8217; families of functions, and complex functions (which are functions of functions), and definite integrals calculated as the difference between two integrals; and so on up through topology and tensor analysis and complex numbers and the complex plane and complex conjugates of matrices, etc. etc., the whole enterprise becoming such a towering baklava of abstractions and abstractions of abstractions that you pretty much have to pretend that everything you&#8217;re manipulating is an actual, tangible thing or else you get so abstracted that you can&#8217;t even sharpen your pencil, much less do any math.
</p></blockquote>
<p>- From &#8220;Everything and More &#8211; A Compact History of ∞&#8221; by David Foster Wallace</p>

<p><a href="http://feedads.g.doubleclick.net/~a/nGaBHF0eCuHDkuBqkPTjRXqaIfY/0/da"><img src="http://feedads.g.doubleclick.net/~a/nGaBHF0eCuHDkuBqkPTjRXqaIfY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/nGaBHF0eCuHDkuBqkPTjRXqaIfY/1/da"><img src="http://feedads.g.doubleclick.net/~a/nGaBHF0eCuHDkuBqkPTjRXqaIfY/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=s-vEjAs3JFY:qIbKuIrrfp8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=s-vEjAs3JFY:qIbKuIrrfp8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=s-vEjAs3JFY:qIbKuIrrfp8:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/s-vEjAs3JFY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2011/11/29/abstraction-levels-in-mathematics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2011/11/29/abstraction-levels-in-mathematics/</feedburner:origLink></item>
		<item>
		<title>CoffeeScript Experiments: Magic Date</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/PUpe-6P-BLA/</link>
		<comments>http://www.vijaykiran.com/2011/11/17/coffeescript-experiments-magic-date/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 13:51:03 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[coffeescript]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3464</guid>
		<description><![CDATA[I started learning CoffeeScript recently and wanted to get my hands dirty. Here&#8217;s the first experiment. ########################################################################################## # Magic Date # # Author: Vijay Kiran&#60;mail@vijaykiran.com&#62; # # Allows conversion of natural date string to real date object. # e.g. tomorrow, today, 2w etc. # # # Implementation is shame-lessly based on MagicDate in Python. ########################################################################################## [...]]]></description>
			<content:encoded><![CDATA[<p>I started learning CoffeeScript recently and wanted to get my hands dirty. Here&#8217;s the first experiment. </p>
<p><span id="more-3464"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">##########################################################################################</span>
<span style="color: #808080; font-style: italic;">#                                 Magic Date</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Author: Vijay Kiran&lt;mail@vijaykiran.com&gt;</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Allows conversion of natural date string to real date object.</span>
<span style="color: #808080; font-style: italic;"># e.g. tomorrow, today, 2w etc.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Implementation is shame-lessly based on MagicDate in Python. </span>
<span style="color: #808080; font-style: italic;">##########################################################################################</span>
<span style="color: #808080; font-style: italic;">##                        Requirements/Specs                                            ##</span>
<span style="color: #808080; font-style: italic;">##                                                                                      ##</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># One liner - should work like OmniFocus date field</span>
<span style="color: #808080; font-style: italic;">## </span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Details:</span>
<span style="color: #808080; font-style: italic;"># • 2d, -3w, 1h, 1y1m, and so on — Relative dates and times put the date at</span>
<span style="color: #808080; font-style: italic;"># a certain amount of time from right now. Negative numbers represent times in the past.</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># • 2 days, -3 weeks, 1 hour, 1 year 1 month, and so on — </span>
<span style="color: #808080; font-style: italic;"># You can use the full names of units too.</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># • yesterday, tomorrow, next thursday, last month, this friday, and so on — </span>
<span style="color: #808080; font-style: italic;">#   You can refer to relative dates using common words. </span>
<span style="color: #808080; font-style: italic;">#   “This”, “next”, and “last” have specific meanings: this friday always means </span>
<span style="color: #808080; font-style: italic;">#   the Friday in this week, next friday always means </span>
<span style="color: #808080; font-style: italic;">#   the Friday in the next week, and last friday always means </span>
<span style="color: #808080; font-style: italic;">#   the Friday in last week, regardless of what day today is. </span>
<span style="color: #808080; font-style: italic;">#   Other units work in the same way.</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># • september, thurs, 2019, and so on — If you enter the name of a specific time period, </span>
<span style="color: #808080; font-style: italic;">#   the date will be at its beginning. So september means September first.</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># • 5/23/08 10a, 9.30.09 2:00 PM, and so on — You can use the short date format as </span>
<span style="color: #808080; font-style: italic;">#   defined in your International system preferences. </span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># • 2w sat, 4d @ 5p, tues 6a, aug 6 tues 5p, and so on —</span>
<span style="color: #808080; font-style: italic;">#    Mix the available formats however you like.</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># • now, 9, 14:00, tom, and so on — OmniFocus makes its best guess </span>
<span style="color: #808080; font-style: italic;">#   at things like bare numbers, times, and word fragments. </span>
<span style="color: #808080; font-style: italic;">#   If you think something might work, give it a try.</span>
&nbsp;
weekdays = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'monday'</span>, <span style="color: #483d8b;">'tuesday'</span>, <span style="color: #483d8b;">'wednesday'</span>, <span style="color: #483d8b;">'thursday'</span>, <span style="color: #483d8b;">'friday'</span>, <span style="color: #483d8b;">'saturday'</span>, <span style="color: #483d8b;">'sunday'</span><span style="color: black;">&#93;</span>
&nbsp;
magicDate = <span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#41;</span> -<span style="color: #66cc66;">&gt;</span>
 d = <span style="color: #dc143c;">new</span> Date
 regs = <span style="color: black;">&#91;</span>
   <span style="color: #808080; font-style: italic;"># Today</span>
   <span style="color: black;">&#91;</span> /^tod/i, Date <span style="color: black;">&#93;</span>,
   <span style="color: #808080; font-style: italic;"># Tomorrow</span>
   <span style="color: black;">&#91;</span> /^tom/i, -<span style="color: #66cc66;">&gt;</span> d.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span>d.<span style="color: black;">getDate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">;</span>  d <span style="color: black;">&#93;</span>,
   <span style="color: #808080; font-style: italic;"># Now</span>
   <span style="color: black;">&#91;</span> /^now/i, Date<span style="color: black;">&#93;</span>,
   <span style="color: #808080; font-style: italic;"># Yesterday</span>
   <span style="color: black;">&#91;</span> /^yes/i, -<span style="color: #66cc66;">&gt;</span> d.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span>d.<span style="color: black;">getDate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">;</span> d <span style="color: black;">&#93;</span>,
   <span style="color: black;">&#91;</span> /^<span style="color: black;">&#40;</span>last|next|this<span style="color: black;">&#41;</span>\s<span style="color: black;">&#40;</span>week<span style="color: black;">&#41;</span>/, 
     <span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span> -<span style="color: #66cc66;">&gt;</span>
       switch result<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">toLowerCase</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
         when <span style="color: #483d8b;">&quot;last&quot;</span> then d.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span>d.<span style="color: black;">getDate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>
         when <span style="color: #483d8b;">&quot;this&quot;</span> then d.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span>d.<span style="color: black;">getDate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
         when <span style="color: #483d8b;">&quot;next&quot;</span> then d.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span>d.<span style="color: black;">getDate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>
       d
   <span style="color: black;">&#93;</span>,
    <span style="color: #808080; font-style: italic;"># &lt;last&gt; &lt;this&gt;/&lt;next&gt; &lt;weekday&gt;</span>
    <span style="color: black;">&#91;</span>
      /^<span style="color: black;">&#40;</span>last|this|next<span style="color: black;">&#41;</span>\s<span style="color: black;">&#40;</span>\S+<span style="color: black;">&#41;</span>/i,
      <span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span> -<span style="color: #66cc66;">&gt;</span>
        rel = result<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">toLowerCase</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        weekday = result<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>.<span style="color: black;">toLowerCase</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        today = d.<span style="color: black;">getDay</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># Pick index + 1 because getDay is 1 = Monday, 2 = Tuesday and so on</span>
        index = <span style="color: black;">&#40;</span>i + <span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">for</span> item, i <span style="color: #ff7700;font-weight:bold;">in</span> weekdays when <span style="color: #dc143c;">new</span> RegExp<span style="color: black;">&#40;</span>weekday<span style="color: black;">&#41;</span>.<span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span>item<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        switch result<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">toLowerCase</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
          when <span style="color: #483d8b;">&quot;last&quot;</span> then d.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span>d.<span style="color: black;">getDate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - <span style="color: black;">&#40;</span>today - index + <span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>index <span style="color: #66cc66;">&lt;</span> today <span style="color: black;">&#41;</span> then <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
          when <span style="color: #483d8b;">&quot;this&quot;</span> then  d.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span>d.<span style="color: black;">getDate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + index - today<span style="color: black;">&#41;</span>
          when <span style="color: #483d8b;">&quot;next&quot;</span> then  d.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span>d.<span style="color: black;">getDate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + index + today - <span style="color: #ff4500;">1</span> <span style="color: black;">&#41;</span>
        d
    <span style="color: black;">&#93;</span>
   <span style="color: black;">&#93;</span>
<span style="color: #808080; font-style: italic;"># </span>
 <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> regs
     <span style="color: #dc143c;">re</span>  = x<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> 
     fn  = x<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
     <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: #dc143c;">test</span> <span style="color: #008000;">str</span> then <span style="color: #ff7700;font-weight:bold;">return</span> fn<span style="color: black;">&#40;</span><span style="color: #008000;">str</span>.<span style="color: black;">match</span> <span style="color: #dc143c;">re</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And here&#8217;s how you can use it:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">testStrings = <span style="color: black;">&#91;</span> <span style="color: #483d8b;">&quot;yes&quot;</span>, <span style="color: #483d8b;">&quot;yesterda&quot;</span>, <span style="color: #483d8b;">&quot;Yes&quot;</span>, <span style="color: #483d8b;">&quot;ye&quot;</span>, <span style="color: #483d8b;">&quot;-&quot;</span>
                <span style="color: #483d8b;">&quot;tod&quot;</span>, <span style="color: #483d8b;">&quot;todd&quot;</span>, <span style="color: #483d8b;">&quot;today&quot;</span>, <span style="color: #483d8b;">&quot;TodaY&quot;</span>, <span style="color: #483d8b;">&quot;-&quot;</span>
                <span style="color: #483d8b;">&quot;now&quot;</span>, <span style="color: #483d8b;">&quot;nowww&quot;</span>, <span style="color: #483d8b;">&quot;Now&quot;</span>, <span style="color: #483d8b;">&quot;ANDNOW&quot;</span>,<span style="color: #483d8b;">&quot;-&quot;</span>
                <span style="color: #483d8b;">&quot;tom&quot;</span>, <span style="color: #483d8b;">&quot;tomorrow&quot;</span>, <span style="color: #483d8b;">&quot;Tomorrow&quot;</span>, <span style="color: #483d8b;">&quot;to&quot;</span>, <span style="color: #483d8b;">&quot;-&quot;</span>  
                <span style="color: #483d8b;">&quot;last mon&quot;</span>, <span style="color: #483d8b;">&quot;last tuesday&quot;</span>, <span style="color: #483d8b;">&quot;last wed&quot;</span>, <span style="color: #483d8b;">&quot;last thurs&quot;</span>,  <span style="color: #483d8b;">&quot;last fri&quot;</span>, <span style="color: #483d8b;">&quot;last sat&quot;</span> , <span style="color: #483d8b;">&quot;LAst Sunda&quot;</span>, <span style="color: #483d8b;">&quot;-&quot;</span>
                <span style="color: #483d8b;">&quot;last week&quot;</span>, <span style="color: #483d8b;">&quot;tHIS Week&quot;</span>, <span style="color: #483d8b;">&quot;next week&quot;</span>, <span style="color: #483d8b;">&quot;-&quot;</span>
                <span style="color: #483d8b;">&quot;this mon&quot;</span>, <span style="color: #483d8b;">&quot;this tuesday&quot;</span>, <span style="color: #483d8b;">&quot;this weDNE&quot;</span>, <span style="color: #483d8b;">&quot;this thursday&quot;</span>, <span style="color: #483d8b;">&quot;THIS FRIDAY&quot;</span>, <span style="color: #483d8b;">&quot;this sat&quot;</span>, <span style="color: #483d8b;">&quot;this sun&quot;</span>, <span style="color: #483d8b;">&quot;-&quot;</span>
                <span style="color: #483d8b;">&quot;next mon&quot;</span>, <span style="color: #483d8b;">&quot;NExt tue&quot;</span>, <span style="color: #483d8b;">&quot;nex wednesday&quot;</span>, <span style="color: #483d8b;">&quot;next thursday&quot;</span>, <span style="color: #483d8b;">&quot;Next Friday&quot;</span>, <span style="color: #483d8b;">&quot;Next Satur&quot;</span>, <span style="color: #483d8b;">&quot;next sun&quot;</span>, <span style="color: #483d8b;">&quot;-&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #008000;">str</span> <span style="color: #ff7700;font-weight:bold;">in</span> testStrings
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">str</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #483d8b;">&quot;-&quot;</span> 
    console.<span style="color: black;">log</span> <span style="color: #483d8b;">&quot;---------------------------------------------------&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">else</span>
    console.<span style="color: black;">log</span> <span style="color: #483d8b;">&quot;#{str}           =     #{magicDate str}&quot;</span></pre></div></div>


<p><a href="http://feedads.g.doubleclick.net/~a/v6eui0IUAson59-0NZPZvHvLb8c/0/da"><img src="http://feedads.g.doubleclick.net/~a/v6eui0IUAson59-0NZPZvHvLb8c/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/v6eui0IUAson59-0NZPZvHvLb8c/1/da"><img src="http://feedads.g.doubleclick.net/~a/v6eui0IUAson59-0NZPZvHvLb8c/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=PUpe-6P-BLA:NeVRnTtIF_E:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=PUpe-6P-BLA:NeVRnTtIF_E:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=PUpe-6P-BLA:NeVRnTtIF_E:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/PUpe-6P-BLA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2011/11/17/coffeescript-experiments-magic-date/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2011/11/17/coffeescript-experiments-magic-date/</feedburner:origLink></item>
		<item>
		<title>Kiva 6 year anniversary celebrations in Paris</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/P_aPj1xzGN0/</link>
		<comments>http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 19:55:44 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[ikiva]]></category>
		<category><![CDATA[kiva]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3441</guid>
		<description><![CDATA[Yesterday I hopped on the train for a quick trip to Paris, to join the Kiva&#8216;s 6th birthday celebrations. I had a great time meeting other Kiva supporters and the trip was totally worth the travel. Here are some photos I took at the event. Also, yesterday &#8211; the iKiva app that I built crossed [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I hopped on the train for a quick trip to Paris, to join the <a href="http://kiva.org">Kiva</a>&#8216;s 6th birthday <a href="http://www.kiva.org/updates/kiva/2011/10/09/global-celebration-in-10-cities-in.html">celebrations</a>. I had a great time meeting other Kiva supporters and the trip was totally worth the travel. </p>
<p>Here are some photos I took at the event.</p>
<p><span id="more-3441"></span></p>

<a href='http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/kiva6-paris9/' title='Kiva6-paris9'><img width="170" height="170" src="http://www.vijaykiran.com/wp-content/uploads/2011/10/Kiva6-paris9-170x170.jpg" class="attachment-thumbnail" alt="Kiva6-paris9" title="Kiva6-paris9" /></a>
<a href='http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/kiva6-paris24/' title='Kiva6-paris24'><img width="170" height="170" src="http://www.vijaykiran.com/wp-content/uploads/2011/10/Kiva6-paris24-170x170.jpg" class="attachment-thumbnail" alt="Kiva6-paris24" title="Kiva6-paris24" /></a>
<a href='http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/kiva6-paris26/' title='Kiva6-paris26'><img width="170" height="170" src="http://www.vijaykiran.com/wp-content/uploads/2011/10/Kiva6-paris26-170x170.jpg" class="attachment-thumbnail" alt="Kiva6-paris26" title="Kiva6-paris26" /></a>
<a href='http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/kiva6-paris27/' title='Kiva6-paris27'><img width="170" height="170" src="http://www.vijaykiran.com/wp-content/uploads/2011/10/Kiva6-paris27-170x170.jpg" class="attachment-thumbnail" alt="Kiva6-paris27" title="Kiva6-paris27" /></a>
<a href='http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/kiva6-paris28/' title='Kiva6-paris28'><img width="170" height="170" src="http://www.vijaykiran.com/wp-content/uploads/2011/10/Kiva6-paris28-170x170.jpg" class="attachment-thumbnail" alt="Kiva6-paris28" title="Kiva6-paris28" /></a>
<a href='http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/kiva6-paris29/' title='Kiva6-paris29'><img width="170" height="170" src="http://www.vijaykiran.com/wp-content/uploads/2011/10/Kiva6-paris29-170x170.jpg" class="attachment-thumbnail" alt="Kiva6-paris29" title="Kiva6-paris29" /></a>
<a href='http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/kiva6-paris30/' title='Kiva6-paris30'><img width="170" height="170" src="http://www.vijaykiran.com/wp-content/uploads/2011/10/Kiva6-paris30-170x170.jpg" class="attachment-thumbnail" alt="Kiva6-paris30" title="Kiva6-paris30" /></a>
<a href='http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/kiva6-paris31/' title='Kiva6-paris31'><img width="170" height="170" src="http://www.vijaykiran.com/wp-content/uploads/2011/10/Kiva6-paris31-170x170.jpg" class="attachment-thumbnail" alt="Kiva6-paris31" title="Kiva6-paris31" /></a>
<a href='http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/kiva6-paris32/' title='Kiva6-paris32'><img width="170" height="170" src="http://www.vijaykiran.com/wp-content/uploads/2011/10/Kiva6-paris32-170x170.jpg" class="attachment-thumbnail" alt="Kiva6-paris32" title="Kiva6-paris32" /></a>

<p>Also, yesterday &#8211; the <a href="http://www.vijaykiran.com/projects/ikiva/">iKiva</a> app that I built crossed 6000 Downloads! I&#8217;m working on the next version which will be an universal app that will work on both iPad and iPhone/iPod touch. And Hopefully I&#8217;ll get enough free time to complete it in the next couple of months.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/YORu50LpGDZisXQOAlFEYkeLd2E/0/da"><img src="http://feedads.g.doubleclick.net/~a/YORu50LpGDZisXQOAlFEYkeLd2E/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/YORu50LpGDZisXQOAlFEYkeLd2E/1/da"><img src="http://feedads.g.doubleclick.net/~a/YORu50LpGDZisXQOAlFEYkeLd2E/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=P_aPj1xzGN0:jR-bOEJ6mac:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=P_aPj1xzGN0:jR-bOEJ6mac:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=P_aPj1xzGN0:jR-bOEJ6mac:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/P_aPj1xzGN0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2011/10/21/kiva-6-years-paris/</feedburner:origLink></item>
		<item>
		<title>→ “My belly is too much swelling with jackfruit”</title>
		<link>http://feedproxy.google.com/~r/Vijaykirancom/~3/cm1geLN06mY/</link>
		<comments>http://www.vijaykiran.com/2011/10/19/my-belly-is-too-much-swelling-with-jackfruit/#comments</comments>
		<pubDate>Wed, 19 Oct 2011 15:18:46 +0000</pubDate>
		<dc:creator>Vijay Kiran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fun]]></category>

		<guid isPermaLink="false">http://www.vijaykiran.com/?p=3439</guid>
		<description><![CDATA[In 1909, after missing his train due to an ultimately disastrous trip to the lavatory at Ahmedpur station, an embarrassed, angry young man named Okhil Chandra Sen sent an unintentionally amusing letter of complaint to the Sahibganj divisional railway office in West Bengal. The letter proved to be an important one as, according to the [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>In 1909, after missing his train due to an ultimately disastrous trip to the lavatory at Ahmedpur station, an embarrassed, angry young man named Okhil Chandra Sen sent an unintentionally amusing letter of complaint to the Sahibganj divisional railway office in West Bengal. The letter proved to be an important one as, according to the Railway Museum in New Delhi, the subsequent investigation into the affair by the British Raj resulted in the introduction of toilets to all trains in the country; something that had been absent since the formation of Indian Railways in 1857.
</p></blockquote>
<p>via <a href="http://www.lettersofnote.com/2011/10/my-belly-is-too-much-swelling-with.html">Letters of Note: My belly is too much swelling with jackfruit</a>.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/_yW2F9yRAIaZKN9sW0tlTVp6rhU/0/da"><img src="http://feedads.g.doubleclick.net/~a/_yW2F9yRAIaZKN9sW0tlTVp6rhU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/_yW2F9yRAIaZKN9sW0tlTVp6rhU/1/da"><img src="http://feedads.g.doubleclick.net/~a/_yW2F9yRAIaZKN9sW0tlTVp6rhU/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=cm1geLN06mY:zUF-likM4pU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=cm1geLN06mY:zUF-likM4pU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Vijaykirancom?a=cm1geLN06mY:zUF-likM4pU:6W8y8wAjSf4"><img src="http://feeds.feedburner.com/~ff/Vijaykirancom?d=6W8y8wAjSf4" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Vijaykirancom/~4/cm1geLN06mY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.vijaykiran.com/2011/10/19/my-belly-is-too-much-swelling-with-jackfruit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.vijaykiran.com/2011/10/19/my-belly-is-too-much-swelling-with-jackfruit/</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: basic

Served from: www.vijaykiran.com @ 2012-05-10 22:34:12 -->

