Writing web applications in Clojure
Templating
The project.clj
file defines two other dependencies: Hiccup [11] and Enlive [12]. Both are templating libraries, but each takes a different approach.
Listing 3 makes Hiccup available, and app
has now changed: The application no longer uses the "Hello, World!" string, but calls the root
function, which is defined in the line above. It uses Hiccup to output HTML. An interesting feature of Clojure is that the developer can modify parts of the code without touching the rest of the virtual machine. Because the task of creating HTML code is outsourced to a separate function, the developer can change it and immediately see the effect on the application.
Listing 3
src/webapp/core.clj
Good Day!
You could change the greeting to "Good day," for example. If the REPL is already in the webapp.core
namespace, the defined aliases are available:
webapp.core=> (hiccup/html [:h1 [:em "Good Day"] ", Hiccup!"]) "<h1>Good Day,, Hiccup!</h1>"
The return value looks good – the following code stores the change in src/webapp/core.clj
:
(defn root [] (hiccup/html [:h1 [:em "Good Day"] ",Hiccup!"]))
The next input reloads the namespace after saving the file:
webapp.core=> (require 'webapp.core :reload) nil
The developer does not need to restart the server. The new behavior of the root
function should be visible after reloading the page in the browser.
Hiccup uses a typical Clojure-style approach to generating HTML, by mapping the data into simple structures. Because common types such as lists, vectors, maps, and sets have a common programming interface, Hiccup templates can be conveniently processed with the usual Clojure functions.
If you work with web designers who are used to working with HTML, Hiccup is not much fun. For this scenario, you will probably want to use the Enlive templating library instead. A simple template, stored as a file in resources/templates/index.html
, is shown in Listing 4.
Listing 4
resources/templates/index.html
The new version of src/webapp/core.clj
in Listing 5 uses :require
to include the Enlive library. The deftemplate
function replaces the previous definition of root
and applies the template. It takes no arguments and simply pushes Hello, Enlive!
into the <h1>
HTML element of the template. To make the change effective, simply reload the namespace in REPL and the page in the browser.
Listing 5
src/webapp/core.clj
If you would like, you can continue these experiments with REPL on your own. To do so, change a little something – maybe break something – and fix it again. Refer to the useful REPL documentation and source code for information on functions. The clojure.repl/doc
and clojure.repl/source
commands are used for this, as the sample session in Listing 6 shows.
Listing 6
Documentation and Source Code
A Chat Application
Now that you've taken your first steps with Clojure, I'll move on to a webchat app that uses WebSockets. The sample application consists of only four files. project.clj
(Listing 7) contains the known list of dependencies: Clojure itself and three libraries. However, the :main
key makes the following webapp.main
the main namespace of the application.
Listing 7
project.clj
The nested map next to :profiles
stipulates that Leiningen should compile all namespaces to create Java class files :aot
(ahead of time) before executing. These two settings make it possible to export the application as a JAR archive later on.
The webapp.core
namespace, as shown in Listing 8, has changed substantially, however. Among the required namespaces (:require
) you will now also find the wrap-params
function from ring.middleware.params
. With its help, the application can access parameters from HTTP requests in the form of a hash.
Listing 8
src/webapp/core.clj
Even the root
template in line 7 is different. It now takes request
as its only argument. If the template is invoked, it searches the HTML object tree for the CSS selector #name
. After finding this, it sets the appropriate value from the request
hash, which has the following structure:
{:params {"name" "<sought after value>"}}
From line 37 onward, the listing defines the app
function. This first creates an atom (a container that stores other objects) that comprises an empty set and is bound to the symbol channels
. This atom will store references to all connections to the chat room and can only be changed by atomic transactions. Thus, a change is either completed, or Clojure retries.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
Fedora 39 Beta is Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.
-
Star Labs Reveals a New Surface-Like Linux Tablet
If you've ever wanted a tablet that rivals the MS Surface, you're in luck as Star Labs has created such a device.
-
SUSE Going Private (Again)
The company behind SUSE Linux Enterprise, Rancher, and NeuVector recently announced that Marcel LUX III SARL (Marcel), its majority shareholder, intends to delist it from the Frankfurt Stock Exchange by way of a merger.