2. Leiningen- a building tool...
● Clojure requires the Java Virtual Machine (JVM) to run
● Clojure applications can be built with the standard Java tools, such as Maven, Ant or
Leiningen.
● Leiningen is a one-stop shop for all your project-management-related needs.
● Leiningen is compatible with Maven, so it has access to large and well-maintained
repositories of Java libraries.
● Clojure libraries are commonly found in the Clojures repository. This repository is, enabled
by default in Leiningen.
3. Installation...
1. Download the Lein script-
https://raw.github.com/technomancy/leiningen/stable/bin/lein
2.Place it on your $PATH where your shell can find it- mv lein ~/bin
3.Set it to be executable- chmod +x lein
4.Run it (lein) and it will download the self-install package – lein self-install
5.lein new luminus example
cd example
lein ring server
//for h2 supported database
lein new luminus example +h2
cd example
(will contain project.clj file with declaration of the project (including dependencies on
Clojure), the README file with template of project's description, and two directories — src
and test for source code & tests. Now you can start to work with you project.)
4. Light Table(Clojure IDE)...
“the next generation code editor”
download Link- http://www.lighttable.com/.(and upload your project)
5. Template Tree Structure....Procfile
README.md
project.clj -used for configuring and building the application
src
└ log4j.xml
guestbook
└ handler.clj -handler that’s going to handle all the requests to it.
util.clj -
repl.clj
models -The models namespace is used to define the model for the application and handle the persistence layer.
└ db.clj -used to house the functions for interacting with the database
schema.clj -used to define the connection parameters and the database tables
routes -The routes namespace is where the routes and controllers for our homepage are located
└ home.clj
views -The views namespace defines the visual layout of the application.
└ layout.clj
└ templates
└ about.html
base.html
home.html
test
└ guestbook
└ test
└ handler.clj
resources -we put all the static resoruces for our application.
└ public
└ css
└ bootstrap-theme.min.css
bootstrap.min.css
screen.css
fonts
└ glyphicons-halflings-regular.eot
glyphicons-halflings-regular.svg
glyphicons-halflings-regular.ttf
glyphicons-halflings-regular.woff
img
js
└ bootstrap.min.css
md
└ docs.md
6. Luminus API...
● Selmer - HTML Templating
● Hiccup – HTML Templating
● Compojure – Accessing Database
● Ring – Accessing Database
● lib Noir – Validation, Session handling, etc.
● SQL Korma – Tasty SQL for Clojure
7. Selmer compiles the template files and replaces any tags with the corresponding functions for handling dynamic content.
Selmer separates the presentation logic from the program logic.
Step1: HTML templating using selmer- (handling dynamic content)
<html>
<head>
<title>My First Template</title>
</head>
<body>
<h2>Hello {{name}}</h2>
<h2>E-mail {{e-mail}}</h2>
</body>
</html>
The templates are rendered using a context represented by a map of key/value pairs. {{name}}- variables that we render during the run time.
Step2: Renderig the above variable at runtime using render-file
(ns example.routes.home
(:use [selmer.parser :only [render-file]]))
(defn index [request]
(render-file "example/views/templates/index.html"
{:name "John" :e-mail ”testing@example.com”}))
pass in a collection we can iterate it using the for tag:
<ul>
{% for item in items %}
<li> {{item}} </li>
{% endfor %}
</ul>
(render-file "/example/views/templates/items.html
{:items (range 10)})
Selmer...
8. Selmer Template inheritance...
The extends tag or the include tag:
Extending Template-
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
Including Template-
<body>
<div id="content">
{% if user %}
{% include "templates/home.html" %}
{% else %}
{% include "templates/login.html" %}
{% endif %}
</div>
9. Hiccup...
Hiccup is a popular HTML templating engine for Clojure. The advantage of using Hiccup
is that it uses Clojure to generate and manipulate our markup. This means that you don't
have to learn a separate DSL for generating your HTML with its own rules.
In Hiccup, HTML elements are represented by Clojure vectors:
Syntax: [:tag-name {:attribute-key "attribute value"} tag-body]
Example:
[:div {:id "test", :class "example"} [:p "Hello world!"]]
which corresponds to the following HTML:
<div id="text" class="example"><p>Hello world!</p></div>
Also, the same can be coded as-
[:div#test.example [:p "Hello world!"]]
10. HTML Templating with Hiccup...
Hiccup helpers for creating HTML forms for our Web Application-
(form-to [:post "/login"]
(text-field {:placeholder "name"} "id")
(password-field {:placeholder "password"} "pass")
(submit-button "login"))
Is Equivalent to:
<form action="/login" method="POST">
<input id="id" name="id" placeholder="name" type="text" />
<input id="pass" name="pass" placeholder="password" type="password" />
<input type="submit" value="login" />
</form>
11. Ring & Compojure...
● Compojure is the library that maps URLs to functions in our program(define application routes).
● Compojure provides a defroute macro which can group several routes together and bind them to a symbol-
(defroutes auth-routes
(POST "/login" [id pass] (login id pass))
(POST "/logout" [] (logout)))
or, It is also possible to group routes by common path elements using context
(def home-routes
(context "/user/:id" [id]
(GET "/profile" [] ...)
(GET "/settings" [] ...)
(GET "/change-password" [] ...)))
* After defining the routes, they are added to the routes vector in handler file.
12. Ring & Compojure...(continued)
Ring is a basic web framework. We can compare it to Servlets in JavaEE. It provides
basic functionality for handling requests, but isn't very high level or full of features. In
particular, it requires to write routes.
Ring is low level API for web application development. For example, it uses maps data
structures to wrap request and response.
Compojure provides an elegant routing library. Most of time, it is used with Ring.
Ring is a basic web framework. We can compare it to Servlets in JavaEE. It provides
basic functionality for handling requests, but isn't very high level or full of features. In
particular, it requires to write routes.
Ring is low level API for web application development. For example, it uses maps data
structures to wrap request and response.
Compojure provides an elegant routing library. Most of time, it is used with Ring.
13. Restricted Access...(uses noir library)
//Making Routes Restricted:
(defroutes private-pages
(GET "/profile" [ ] (restricted (show-profile)))
(GET "/secret-page1" [ ] (restricted (show-secret-page)))
(GET "/secret-page2" [ ] (restricted (another-secret-page))))
noir.util.route/restricted macro is used to indicated that access rules apply to the
route.
For marking multiple routes as restricted we use:
(def-restricted-routes private-pages())
//Making Routes Restricted:
(defroutes private-pages
(GET "/profile" [ ] (restricted (show-profile)))
(GET "/secret-page1" [ ] (restricted (show-secret-page)))
(GET "/secret-page2" [ ] (restricted (another-secret-page))))
noir.util.route/restricted macro is used to indicated that access rules apply to the
route.
For marking multiple routes as restricted we use:
(def-restricted-routes private-pages())
14. Adding the Data Models...
Step3: All the dependencies for mentioned API's are managed via updating the project.clj file:
:dependencies
[[ring-server "0.3.1"]
[com.h2database/h2 "1.3.175"]
[korma "0.3.0-RC6"]
[org.clojure/clojure "1.6.0"]
[selmer "0.6.5"]
[lib-noir "0.8.1"]
[compojure "1.1.6"]]
:plugins [[lein-ring "0.8.10"] [lein-environ "0.4.0"]]
Step4: creating a database model in schema.clj file: (defination in the form of map)
(def db-spec {:classname "org.h2.Driver"
:subprotocol "h2"
:subname (str (io/resource-path) db-store)
:user "sa"
:password ""
:naming {:keys clojure.string/upper-case
:fields clojure.string/upper-case}})
15. Next, to write a function creating the database table schema.
(defn create-users-table [ ]
(sql/with-connection db-spec
(sql/create-table
:users[
:id “varchar(5)”
:e-mail “varchar(30)”])))
now, a calling function for table:
(defn create-table [ ]
(create-users-table))
Step5: creating routes for the html pages using above described compojures and also the associated
functions having the validations/functionalities.