I need to read a clojure function from an edn file which outputs hiccup to generate html content. But I'm stuck at the part where the function needs to be evaluated. I receive the error message:
java.lang.RuntimeException: Unable to resolve symbol: fn in this context, compiling:(null:1:1)
((eval (read-string "(fn [] (list [:div\"Hello\"]))")))
and
((load-string "(fn [] (list [:div\"Hello\"]))"))
are working inside a clojure REPL and output the expected result
([:div "Hello"])
project.clj
(defproject infocenter "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/clojurescript "1.9.225"]
[hiccup "1.0.5"]]
:plugins [[lein-figwheel "0.5.4-7"]]
:clean-targets ^{:protect false} [:target-path "out" "resources/public/cljs"]
:cljsbuild {
:builds [{:id "dev"
:source-paths ["src"]
:figwheel true
:compiler {:main "templ.core"
:asset-path "cljs/out"
:output-to "resources/public/cljs/main.js"
:output-dir "resources/public/cljs/out"}}]}
:figwheel {
:css-dirs ["resources/public/template"]})
core.cljs
(ns templ.core
(:require-macros [templ.edn :refer [read-edn]]))
(let [div (. js/document getElementById "content")]
(set! (. div -innerHTML) (read-edn "fn.edn")))
edn.clj
(ns templ.edn
(:use [hiccup core form]))
(defmacro read-edn
"Read template from file in resources/"
[edn-name]
;(slurp edn-name)
;((eval (read-string "(fn [] (list [:div\"Hello\"]))")))
((load-string "(fn [] (list [:div\"Hello\"]))"))
)
Try this:
(ns xyz.core
(:gen-class))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "main - enter")
(println (eval '(+ 2 3)))
(println ((eval (read-string "(fn [] (list [:div\"Hello\"]))"))))
(println "main - exit")
)
with results:
> lein run
main - enter
5
([:div Hello])
main - exit
This is just plain Clojure, and I see you are using CLJS as well. There may be some problem in your setup with the CLJS part of it. What are you trying to do with read-edn
and fn.edn
? I would avoid macros completely whenever possible.