Home > enlive-samples

enlive-samples

Enlive-samples is a project mainly written in Clojure, it's free.

Attempt to provide simple examples of enlive (Clojure templating library)

+TITLE: Enlive Samples

+STARTUP: indent

  • What's this ?

    [[https://github.com/cgrand/enlive][Enlive]] is a great templating framework for Clojure, but it is also mind bending.

    enlive-samples tries to provide very simple examples, to be used as a starting point in other projects

  • Beware

| [[https://github.com/denlab/enlive-samples/raw/master/src/doc/image/danger.png]] | Under construction! Not ready yet! | |------------------------------------------------------------------------------+---|

  • Running the project

+BEGIN_SRC sh

cd enlive-samples

+END_SRC

** Get the dependencies:

+BEGIN_SRC sh

lein deps

+END_SRC

** Start a REPL:

+BEGIN_SRC sh

lein repl

+END_SRC

or with emacs:

+BEGIN_SRC sh

lein swank

+END_SRC

** At the REPL:

+BEGIN_SRC clojure

(ns user (:require [net.cgrand.enlive-html :as html]) (:use [enlivesamples.sample3] [clojure.contrib.repl-utils :only [show]] [clojure.test :only [run-tests]] [clojure.repl] [enlivesamples.utils]))

+END_SRC

** Now point your browser at http://localhost:8080

  • Sample 1: Replace the content of a tag.

We will just replace the content of the tag with =id= message.

Here is a diagram of the sample 1 that tries ;) to explain how the pieces work together.

[[https://github.com/denlab/enlive-samples/raw/master/src/doc/image/diagram-sample1.png]]

  • Sample 2: Replace the content of an attribute.

    We just replace the style of the =div= with =id= = 'message', with a random background color.

  • Sample 3: Repeating elements

Here we simply repeat divs.

** On the REPL

+BEGIN_SRC clojure

(ns user (:require [net.cgrand.enlive-html :as html]) (:use [enlivesamples.sample3] [clojure.contrib.repl-utils :only [show]] [clojure.test :only [run-tests]] [clojure.repl] [enlivesamples.utils]))

+END_SRC

** To test the selector on the REPL:

+BEGIN_SRC clojure

(render (html/emit* (section-model {:title "title" :sections {:sections [{:title "abc"}]}})))

+END_SRC

  • Sample 4: Repeating elements in a container

Here we will repeat components inside a container.

  • Credits

    Code heavily borrowed from [[https://github.com/swannodette/enlive-tutorial][enlive-tutorial]].

  • Resources

    • A great Enlive tutorial: [[https://github.com/swannodette/enlive-tutorial][enlive-tutorial]].