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
Direct Download
Read full article as PDF:
Price $2.95
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
News
-
An All-Snap Version of Ubuntu is In The Works
Along with the standard deb version of the open-source operating system, Canonical will release an-all snap version.
-
Mageia 9 Beta 2 Ready for Testing
The latest beta of the popular Mageia distribution now includes the latest kernel and plenty of updated applications.
-
KDE Plasma 6 Looks to Bring Basic HDR Support
The KWin piece of KDE Plasma now has HDR support and color management geared for the 6.0 release.
-
Bodhi Linux 7.0 Beta Ready for Testing
The latest iteration of the Bohdi Linux distribution is now available for those who want to experience what's in store and for testing purposes.
-
Changes Coming to Ubuntu PPA Usage
The way you manage Personal Package Archives will be changing with the release of Ubuntu 23.10.
-
AlmaLinux 9.2 Now Available for Download
AlmaLinux has been released and provides a free alternative to upstream Red Hat Enterprise Linux.
-
An Immutable Version of Fedora Is Under Consideration
For anyone who's a fan of using immutable versions of Linux, the Fedora team is currently considering adding a new spin called Fedora Onyx.
-
New Release of Br OS Includes ChatGPT Integration
Br OS 23.04 is now available and is geared specifically toward web content creation.
-
Command-Line Only Peropesis 2.1 Available Now
The latest iteration of Peropesis has been released with plenty of updates and introduces new software development tools.
-
TUXEDO Computers Announces InfinityBook Pro 14
With the new generation of their popular InfinityBook Pro 14, TUXEDO upgrades its ultra-mobile, powerful business laptop with some impressive specs.