首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >clojure,enlive,多站点

clojure,enlive,多站点
EN

Stack Overflow用户
提问于 2012-12-08 07:10:32
回答 1查看 405关注 0票数 4

尝试根据:server-name在请求中返回的内容加载特定的模板:

代码语言:javascript
复制
(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。

有没有宏或者不同的方式来处理这个用例?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-24 20:12:43

正如注释中提到的,deftemplate是一个宏,它将模板定义为名称空间中的函数-只在第一次求值时定义一次。您可以轻松地编写一些代码来懒惰地创建模板,并通过在模板创建后对其进行缓存来消除一些开销:

代码语言:javascript
复制
(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,如果您愿意,您可以直接使用它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13772481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档