Reload HTML templates

59 views
Skip to first unread message

Marc-Elian Bégin

unread,
Jun 27, 2014, 3:24:00 PM6/27/14
to enliv...@googlegroups.com
Hi,

I'm trying to implement a template relocation feature and struggling with reloading.  The goal is to give the users the ability to change the location of templates used by enlive defsnippet.

The reload mechanism is as follows.  I've tried piggy backing enlive's reload feature, as well as using the basic require with :reload flag.

(ns slipstream.common.reload

  (:require [net.cgrand.enlive.reload :as enlive-reload]))


(defn- get-name

  "Extract the name as a string"

  [ns]

  (-> ns ns-name name))

  

(defn- ours?

  "Filter namespaces starting with 'slipstream.'"

  [nses]

  (filter

    #(.startsWith (get-name %) "slipstream.ui.views")

    nses))


(defn refresh-views

  "Reload all slipstream.ui.views.* modules. Use :reload and not :reload-all, otherwise the required modules are

   also reloaded, including the slipstream.ui.config module. This last module contains atom that must be altered

   for which views module must be reloaded."

  []

  (let [ours (ours? (all-ns))]

    (doseq [ns ours]

      (let [name (ns-name ns)]

        (try

          (enlive-reload/reload-ns ns)

;          (require name :reload :verbose)

          (catch Exception e (print "Error reloading clojure module " name " with detail: " e)))))))


What throws me is that it works in the repl, but not when I execute this code in a war (using java restlet framework).

The way I'm using this to define the template is as follow:

(def configuration-template-html (common/get-template "configuration.html"))


(def parameters-sel [:#parameters])


(html/defsnippet header-titles-snip configuration-template-html header/titles-sel

  []

  identity)



And the get-template impl:

(ns slipstream.ui.config)


(def template-namespace (atom "toto/titi/tata"))


This is integrated into a function:

(defn -setHtmlTemplateNamespace

  "Set alternative namespace for HTML template. Must be slash separated."

  [ns]

  (common/set-template! ns)

  (reload/refresh-views)

  @slipstream.ui.config/template-namespace)


And exposed to Java using gen-class:

  (:gen-class

    :name slipstream.ui.views.Representation

    :methods [#^{:static true

                 :doc "Set alternative namespace for HTML template. Must be slash separated."}

                [setHtmlTemplateNamespace [String] String]


And called in Java like this:

String toto = Representation.setHtmlTemplateNamespace(namespace);


Tracing through with the debugger, I see that the class is reported as reloaded, and the value of the atom set to the new namespace value, but the template used by the defsnippet remains unchanged.  But if I call the -setHtmlTemplateNamespace function in the repl I get the expected result.

I've checked that the namespace I'm changing to is in the CLASSPATH.

This sounds like a class loader issue, but reading about it and trying a few things didn't yield any ah ha moment :-(

Anything obvious I'm missing?

Marc-Elian Bégin

unread,
Jun 28, 2014, 2:43:55 AM6/28/14
to enliv...@googlegroups.com
As a work around, I’ve reshuffled the order in which Clojure modules are loaded, such that I set the atom before loading the templates. So, I can set a new value and then restart the service to force the complete reload.  It’s not ideal as a restart is required, but at least I’m not stuck anymore.

Understanding this though would make the system more dynamic.

--
You received this message because you are subscribed to the Google Groups "Enlive" group.
To unsubscribe from this group and stop receiving emails from it, send an email to enlive-clj+...@googlegroups.com.
To post to this group, send email to enliv...@googlegroups.com.
Visit this group at http://groups.google.com/group/enlive-clj.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages