proper method of rendering template files contained in jars

This commit is contained in:
Gered 2014-03-23 09:43:05 -04:00
parent f68862e784
commit a84c8588bd
2 changed files with 16 additions and 3 deletions

View file

@ -1,6 +1,7 @@
(ns clj-jtwig.core
"wrapper functions for working with JTwig from clojure"
(:import (com.lyncode.jtwig JtwigTemplate JtwigContext JtwigModelMap)
(com.lyncode.jtwig.resource ClasspathJtwigResource)
(com.lyncode.jtwig.tree.api Content)
(java.io File FileNotFoundException ByteArrayOutputStream)
(java.net URL))
@ -56,9 +57,15 @@
(.compile)))
(defn- compile-template-file [^File file]
(->> file
(new JtwigTemplate)
(.compile)))
(if (inside-jar? file)
(->> (.getPath file)
(get-jar-resource-filename)
(new ClasspathJtwigResource)
(new JtwigTemplate)
(.compile))
(->> file
(new JtwigTemplate)
(.compile))))
(defn- newer? [^File file other-timestamp]
(let [file-last-modified (get-file-last-modified file)]

View file

@ -9,6 +9,12 @@
; the path of a file inside a jar looks something like "jar:file:/path/to/file.jar!/path/inside/jar/to/file"
(.contains "jar!")))
(defn get-jar-resource-filename [^String resource-filename]
(let [pos (.indexOf resource-filename "jar!")]
(if-not (= -1 pos)
(subs resource-filename (+ pos 5))
resource-filename)))
(defn exists? [^File file]
(if (inside-jar? file)
;; TODO: can't use File.exists() for this