尝试根据:server-name在请求中返回的内容加载特定的模板:
(ns rosay.views.common
(:use noir.core)
(:require [noir.request :as req]
[clojure.string :as string]
[net.cgrand.enlive-html :as html]))
(defn get-server-name
"Pulls servername for template definition"
[]
(or (:server-name (req/ring-request)) "localhost"))
(defn get-template
"Grabs template name for current server"
[tmpl]
(string/join "" (concat [(get-server-name) tmpl])))
(html/deftemplate base (get-template "/base.html")
[]
[:p] (html/content (get-template "/base.html")))它适用于本地主机,它返回/home/usr/rosay/resources/localhost/base.html,,但是当我测试一个不同的主机,比如"hostname2“时,我看到get-template在哪里查找/home/usr/rosay/resources/hostname2/base.html,但是当它在浏览器中呈现时,它总是指向回../resources/ localhost /base.html。
有没有宏或者不同的方式来处理这个用例?
发布于 2013-01-24 20:12:43
正如注释中提到的,deftemplate是一个宏,它将模板定义为名称空间中的函数-只在第一次求值时定义一次。您可以轻松地编写一些代码来懒惰地创建模板,并通过在模板创建后对其进行缓存来消除一些开销:
(def templates (atom {}))
(defmacro defservertemplate [name source args & forms]
`(defn ~name [& args#]
(let [src# (get-template ~source)]
(dosync
(if-let [template# (get templates src#)]
(apply template# args#)
(let [template# (template src# ~args ~@forms)]
(swap! templates assoc src# template#)
(apply template# args#)))))))在您的情况下,您将能够说(defservertemplate base "/base.html"...。
你也许可以把它整理一下。您真正需要知道的是,deftemplate只调用template,如果您愿意,您可以直接使用它。
https://stackoverflow.com/questions/13772481
复制相似问题