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
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.