Hello! Even at the very beginning of the study Clojure. Tell me please.

I have a clojure project. I use the hiccup templating engine and Bootstrap styles. Everything is working. But I just don’t understand how the conditional operators work in my case.

Here is my code:

Project.clj file :

(defproject yupppie "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "ссылка (ссылка словом потому что не позволяет репутация вставлять)" :license {:name "Eclipse Public License" :url "ссылка"} :dependencies [[org.clojure/clojure "1.7.0"] [ring "1.4.0"] [compojure "1.4.0"] [hiccup "1.0.5"]] :plugins [[lein-ring "0.9.7"]] :ring {:handler yupppie.core/app}) 

Yupppie.core file:

 (ns yupppie.core (:require [compojure.core :refer :all] [compojure.route :as route] [ring.middleware.params :refer [wrap-params]] [clojure.pprint :refer :all] [hiccup.core :refer :all] [hiccup.page :refer [include-css include-js]])) (defn home [] (html [:head (include-css "/bootstrap-3.3.6-dist/css/bootstrap.min.css") (include-css "/styles.css") (include-css "https://fonts.googleapis.com/icon?family=Material+Icons") (include-js "/bootstrap-3.3.6-dist/js/bootstrap.min.js")] [:body [:div {:class "col-lg-6"} [:div {:class "input-group"} [:input {:type "text" :class "form-control" :placeholder "How old are you?"} [:span {:class "input-group-btn"} [:button {:class "btn btn-default" :type=" button"} "Go, baby!"]]]]]])) ; (defroutes app (route/resources "/") (GET "/" [] (home)) (POST "/saveform" req (with-out-str (clojure.pprint/pprint (:params req))))) (def apps (wrap-params app)) 

I began to study it recently, so I don’t really understand it.

I have a simple form. And when you enter in the field a digit less than 18 and press a button, you need to make it so that one picture is shown, and when you enter a digit 18 or more, another picture. Here's how I use the conditional if statement to make it work? Where to insert the code and what to write?

  • Have you already sent the field value to the server? I do not see the relevant code. - D-side
  • No, not yet done. I am now trying locally. - Piston

1 answer 1

I expect that you will figure out how to send the value in the form. I will assume that you have dealt with this and the necessary value arrives in the parameters to the request.

I will assume that the string with the number sent is available in the parameters under the key age and therefore can be booted up in Compojure, like:

 (POST "/" [age] ???) 

There is a string. We need a number.

You can not reinvent the wheel and tighten the function as-int from compojure.coercions . True, you will need to slightly change the description of the method:

 (POST "/" [age :<< as-int] ???) 

Alternatives? They are not necessary, the implementation of as-int extremely simple:

 (try (Long/parseLong s) (catch NumberFormatException _ nil)) 

Not seeing its source before, I tried to write the conversion code myself. The only difference from as-int was the use of Integer/parseInt . And it was wrong!

Now we consider, whether the user is old enough, and we inform about it in the template

 (let [old-enough? (and (-> age nil? not) ; если число не распарсилось, придёт nil (< 18 age))] (функция-шаблончик old-enough?)) 

In the template ... well, just take and instead of one of the DOM nodes you put if .

 (defn функция-шаблончик [old-enough?] (html [:body (if old-enough? "YEAH" "NOPE")])) 

Examples, of course, are quite primitive. But you can develop them, right? :)

  • Thank! I can develop) - Piston
  • Well, of course, I just started to study it and it is not so easy for me at this stage. But I will try. - Piston
  • @Piston If this is the case, then mark the answer as a solution. Instill in readers confidence that it works :) - D-side
  • Not. I have not figured it out yet. How to earn, I will mark) It is necessary to understand. - Piston