Search code examples
clojureluminus

Custom files in luminus


Where should I use put code files I write myself in a Clojure luminus application? And how can I export a function from it and import it to another file? Say, I've create a file "helper1.clj", how can I access the functions from it in "src/clj/my_app/routes/home.clj"? And where should I put the file "helper1.clj"?


Solution

  • Look at the project.clj file. You will see a line that reads:

    :source-paths ["src/clj"]
    

    This means that the src/clj directory will be the root of all namespaces. The namespace is the directory path separated by dots, with the final portion of the namespace being the file name. An example:

    File name:                 my_app/src/clj/dirone/dirtwo/myfile.clj
    Namespace in this file:    (ns dirone.dirtwo.myfile ...)   
    

    With that now established: you should probably put new files in src/clj/my_app for now. The namespace for helper.clj would then look like:

    (ns my-app.helper ...)
    

    You can create new directories under src/clj, for example, src/clj/newdir. A file in that directory called anotherfile.clj would have a namespace of:

    (ns newdir.anotherfile ...)
    

    Look in your my_app/routes/home.clj file and look at the top, and you will see where :require [my-app.layout :as layout]. You would add the following to reference your function myfunc in your file helper.clj:

    ;... list of items under :require
    [my-app.helper :as h]
    ;...
    
    (def something (h/myfunc ...))