-
-
Save anildigital/fc22629ac2fb6f30e144 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns adder.core | |
(:use compojure.core) | |
(:use hiccup.core) | |
(:use hiccup.page-helpers)) | |
(defn view-layout [& content] | |
(html | |
(doctype :xhtml-strict) | |
(xhtml-tag "en" | |
[:head | |
[:meta {:http-equiv "Content-type" | |
:content "text/html; charset=utf-8"}] | |
[:title "adder"]] | |
[:body content]))) | |
(defn view-input [] | |
(view-layout | |
[:h2 "add two numbers"] | |
[:form {:method "post" :action "/"} | |
[:input.math {:type "text" :name "a" :value a}] [:span.math " + "] | |
[:input.math {:type "text" :name "b" :value b}] [:br] | |
[:input.action {:type "submit" :value "add"}]])) | |
(defn view-output [a b sum] | |
(view-layout | |
[:h2 "two numbers added"] | |
[:p.math a " + " b " = " sum] | |
[:a.action {:href "/"} "add more numbers"])) | |
(defn parse-input [a b] | |
[(Integer/parseInt a) (Integer/parseInt b)]) | |
(defroutes app | |
(GET "/" [] | |
(view-input)) | |
(POST "/" [a b] | |
(let [[a b] (parse-input a b) | |
sum (+ a b)] | |
(view-output a b sum)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment