Enlive snippet in template produces lazy sequence - clojure

Visiting localhost:3001/test results in the following HTML:
<html>
<head>
</head>
<body>clojure.lang.LazySeq#27237276</body>
</html>
Clojure Code:
(ns notebook.handler
(:require [compojure.core :refer :all]
[compojure.handler :as handler]
[compojure.route :as route]
[net.cgrand.enlive-html :as html]))
(html/defsnippet welcome
(html/html [:h1]) ; html snippet
[:h1] ; selector
[username] ; arguments
[:h1] (html/content username)) ; substitution
(html/deftemplate home-page "templates/base.html"
[username]
[:body] (html/html-content (welcome username)))
(defroutes app-routes
(GET "/test" [] (home-page "oru"))
(route/resources "/")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))
It looks like I'm not using templates correctly and/or screwing up laziness somewhere. I've tried placing doall in a few places hoping it'd resolve the laziness but no dice.
A debugging attempt:
(welcome "oru")
=> ({:tag :h1, :attrs {}, :content ("oru")})
(html/emit* (welcome "oru"))
=> ("<" "h1" ">" "oru" "</" "h1" ">")
So far so good...
(home-page "oru")
=> ("<" "html" ">" "\n " "<" "head" ">" "\n " "</" "head" ">" "\n " "<" "body" ">" "clojure.lang.LazySeq#27237276" "</" "body" ">" "\n\n" "</" "html" ">")
Bam! "clojure.lang.LazySeq#27237276", the heck is this doing here?

You want to use content, not html-content, since snippets produce a sequence of nodes. html-content expects a string of literal HTML content, and is likely just calling str on its argument (in this case the lazy sequence that is the output of your snippet).

Related

Clojure and Compojure: Response Map is nil

I'm using compojure for a basic web app, I have this code in core.clj:
(defroutes routes
(GET "/" [] (layout/application "Home" (contents/index)))
(route/resources "/"))
(def application (handler/site routes))
(defn -main []
(let [port (Integer/parseInt (or (System/getenv "PORT") "8090"))]
(jetty/run-jetty application {:port port :join? false})))
When I access the 0.0.0.0:8090 everything is loading ok, but I keep seeing this error:
java.lang.NullPointerException: Response map is nil
at ring.util.servlet$update_servlet_response.invokeStatic(servlet.clj:100)
at ring.util.servlet$update_servlet_response.invoke(servlet.clj:91)
at ring.util.servlet$update_servlet_response.invokeStatic(servlet.clj:95)
at ring.util.servlet$update_servlet_response.invoke(servlet.clj:91)
at ring.adapter.jetty$proxy_handler$fn__337.invoke(jetty.clj:27)
at ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$ff19274a.handle(Unknown Source)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:503)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:364)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126)
at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
at java.base/java.lang.Thread.run(Thread.java:844)
Any idea what's going on?
#Svante is almost certainly correct. An easy way to verify is to use the spyx function from the Tupelo library:
(ns demo.core
(:use tupelo.core))
(defroutes routes
(GET "/" [] (spyx (layout/application "Home" (contents/index))))
(route/resources "/"))
which will print something like:
(layout/application "Home" (contents/index))) => nil
when run. spyx ("spy explicit") prints the expression you give it, an arrow, and the expression value. spy, spyx, spy-pretty, etc also return the value printed (unlike println which always returns nil) so you can insert a spy printout anywhere without disrupting the processing chain. Thus, you don't need to write something like:
(defroutes routes
(GET "/" [] (let [tmp-1 (layout/application "Home" (contents/index))]
(println "layout/application result => " tmp-1)
tmp-1)))
(route/resources "/"))
in order to get a debug message printed. In order to spy & friends, add this to the :dependencies in your project.clj:
[tupelo "0.9.138"]
Update
Hmmmm.... Not sure what could be the problem. I made a simple demo app from lein new compojure demo-compojure with the following:
(ns demo-compojure.handler
(:use tupelo.core)
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]))
(defn index []
(spy :index--result "Hello World"))
(defroutes app-routes
(GET "/" [] (spyx (index)))
(route/not-found "Not Found"))
(def app
(wrap-defaults app-routes site-defaults))
and results:
~/expr/demo-compojure > lein ring server
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Started server on port 3000
:index--result => "Hello World"
(index) => "Hello World"
So that is working. Also, int? is a basic Clojure function, so that is puzzling. Perhaps make a clean demo project like the above and take it from there?

ClojureScript: parsing a Transit response

I started learning ClojureScript this week and I stuck parsing a Transit response, I have this function:
(defn handler [response]
(let [comment (:comment response)
created_at (:created_at response)
last_name (:last_name response)
_ (.log js/console (str ">>> COMMENT >>>>> " comment))
comments_div (.getElementById js/document "comments")]
(.append comments_div comment)
(.log js/console (str "Handler response: " response))))
And the console shows:
So, "response" looks fine but I can't get the content from the "response" map (I think is a map) using:
comment (:comment response) or comment (get response :comment)
The headers say that the response is an "application/transit+json" kind. I tried:
(ns blog.core
(:require [cognitect.transit :as t]))
(def r (t/reader :json))
let [parsed (t/read r response).... <--- inside the let block
but no luck so far. Need I to parse the var "response"?
Since it is not working like a map, it probably is a string. Try checking the type of response. with
(println (type response))
If it is a string then :
(ns example
(:require [clojure.data.json :as json]))
(console.log ((json/read-str response) :comment))
This works fine:
(ns blog.core
(:require [domina :as dom]
[ajax.core :refer [GET POST DELETE]]
[cognitect.transit :as t]
[bide.core :as r]))
(def r (t/reader :json))
(defn handler [response]
(let [parsed (t/read r response)
_ (.log js/console (str ">>> PARSED >>>>> " (type parsed) ">>>>" parsed))
comment (get parsed "comment")
.... rest of the code...

Clojure require namespace: "Don't know how to create ISeq from: clojure.lang.Keyword"

I'm trying to split code in 2 files, each with it's own namespace. Following this tutorial.
But I get this error:
Exception in thread "main" java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Keyword
I think it's because the namespace being included is not recognised properly.
Main file:
(ns mytest2.handler
(:use compojure.core)
(:require [compojure.handler :as handler]
[compojure.route :as route]
[mytest2.views :as foo] ;<-- line causing error
[hiccup.core :refer (html)])
)
(defn layout [title & content]
(html
[:head [:title title]]
[:body content]))
(defn main-page []
(layout "My Blog"
[:h1 "My Blog"]
[:p "Welcome to my page"]))
(defroutes app-routes
(GET "/" [] (main-page))
(route/resources "/")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))
; (println (seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))
Second file:
(ns mytest2.views
:require [hiccup.core :refer (html)]
)
(defn layout [title & content]
(html
[:head [:title title]]
[:body content]))
(defn main-page []
(layout "My Blog"
[:h1 "My Blog"]
[:p "Welcome to my page"]))
(note I copied the functions from mytest2.views in mytest2.handler for testing. They're not supposed to be in mytest2.handler).
Paths of files:
/mytest2/src/mytest2/handler.clj
/mytest2/src/mytest2/views.clj
(where first mytest2 is the name of the project, and the second is part of the path- automatically created by lein).
As you see in the first file I printed the class path to verify that /mytest2/src/mytest2/ is included, and yes it is.
Received the same error from trying to use :refer :all in Clojurescript, which apparently is against the rules.
You missed some brackets in your original code
;; wrong
(ns mytest2.views
:require [hiccup.core :refer [html]])
There is just one pair of brackets missing. Do it as in your Main file:
;; Done right!
(ns mytest2.views
(:require [hiccup.core :refer [html]]))
I am not familar with Compojure so I do not know what you have to require. But you need to add the bracket around :require.

How to set Content-Type header on Ring-Compojure application

I'm trying to get started with Clojure and Clojurescript by implementing a simple web app. Things are going pretty good so far and reading from different tutorials I've come up with the code below:
core.clj:
(ns myapp.core
(:require [compojure.core :as compojure]
[compojure.handler :as handler]
[compojure.route :as route]
[myapp.controller :as controller]))
(compojure/defroutes app-routes
(compojure/GET "/" [] controller/index)
(route/resources "/public")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))
controller.clj:
(ns myapp.controller
(:use ring.util.response)
(:require [myapp.models :as model]
[myapp.templates :as template]))
(defn index
"Index page handler"
[req]
(->> (template/home-page (model/get-things)) response))
templates.clj:
(ns myapp.templates
(:use net.cgrand.enlive-html)
(:require [myapp.models :as model]))
(deftemplate home-page "index.html" [things]
[:li] (clone-for [thing things] (do->
(set-attr 'data-id (:id thing))
(content (:name thing)))))
The problem is I can't display non-ascii characters on the page and I don't know how to set HTTP headers on a page.
I see solutions like this but I simply can't figure out where place them in my code:
(defn app [request]
{:status 200
:headers {"Content-Type" "text/plain"}
:body "Hello World"})
P.S: Any suggestions about style and/or code organization are welcome.
Use ring.util.response:
(require '[ring.util.response :as r])
Then on your index function:
(defn index
"Index page handler"
[req]
(-> (r/response (->> (template/home-page (model/get-things)) response))
(r/header "Content-Type" "text/html; charset=utf-8")))
You can chain other actions on the response such as set-cookie and whatnot:
(defn index
"Index page handler"
[req]
(-> (r/response (->> (template/home-page (model/get-things)) response))
(r/header "Content-Type" "text/html; charset=utf-8")
(r/set-cookie "your-cookie-name"
"" {:max-age 1
:path "/"})))

Clojure Ring: How to print a variable next to a string

I'm trying to set the Environment Variable known as POWERED_BY to the variable message.
Then I'd like to test if message is empty or NULL. Then print "Powered by" message.
Currently, the code below does not work.
(ns helloworld.web
(:use compojure.core [ring.adapter.jetty :only [run-jetty]] )
(:require [compojure.route :as route]
[compojure.handler :as handler]))
(defroutes main-routes
; what's going on
(def message (System/getenv "POWERED_BY"))
(GET "/" [] (apply str "Powered by " message))
(route/resources "/")
(route/not-found "Page not found") )
(def app
(handler/api main-routes))
(defn -main [port]
(run-jetty app {:port (Integer. port)}))
Define message outside routes definition:
(def message (System/getenv "POWERED_BY"))
(defroutes main-routes
; what's going on
(GET "/" [] (str "Powered by " message)
(route/resources "/")
(route/not-found "Page not found"))
In case you want to retrieve the system environment variable value each time the request is received you can use the let form:
(defroutes main-routes
; what's going on
(GET "/" [] (let [message (System/getenv "POWERED_BY")]
(str "Powered by " message))
(route/resources "/")
(route/not-found "Page not found"))
For concat just use (str arg1 arg2 ...), apply works on lists, so if you want to use it you should do something like (apply str ["Powered by" message]) instead.