How can I handle html files in Luminus which aren't in "resources"?

389 views Asked by At

I have this:

(defn about-page []
    (layout/render "about.html" {:title "About"}))

But since I have moved the directory "templates" from "resources" to the root directory and on a server I might put it yet in another place, it doesn't work. I did it because I don't want the html templates to be embedded in the output jar. So how can I make the code work, how can I get access to my html files in "templates" then?

And the same question for static images, css, js: I put them in the root directory for now, so they aren't in "resources". They're in "public" folder. However, when I refer to them as "public/css/css1.css", they aren't getting found, that is, the path localhost:3000/public/css/css1.css doesn't exist.

How can I tell Luminus where my statics are located now?

1

There are 1 answers

4
Piotrek Bzdyl On BEST ANSWER

Templates location

Selmer's documentation describes how to change the location of the templates:

By default the templates are located relative to the ClassLoader URL. If you'd like to set a custom location for the templates, you can use selmer.parser/set-resource-path! to do that:

(selmer.parser/set-resource-path! "/var/html/templates/")

It's also possible to set the root template path in a location relative to the resource path of the application:

(set-resource-path! (clojure.java.io/resource "META-INF/foo/templates"))

This allows the templates to be refrerenced using include and extends tags without having to specify the full path.

To reset the resource path back to the default simply pass it a nil:

(selmer.parser/set-resource-path! nil)

The application will then look for templates at this location. This can be useful if you're deploying the application as a jar and would like to be able to modify the HTML without having to redeploy it.

As you want your templates to be reload when you change them you should also remember that Selmer caches them:

When rendering files Selmer will cache the compiled template. A recompile will be triggered if the last modified timestamp of the file changes. Note that changes in files referenced by the template will not trigger a recompile. This means that if your template extends or includes other templates you must touch the file that's being rendered for changes to take effect.

Alternatively you can turn caching on and off using (selmer.parser/cache-on!) and (selmer.parser/cache-off!) respectively.

Assets location

Handling of static resources is configured using site-defaults in your <app>.middleware namespace. You need to configure its' :static entry to use :files instead:

(-> site-defaults
  (assoc :static {:files "/var/www/html"}))

and you need to copy files from resources/public directory to that location.