clojure,招募,多站点 [英] clojure, enlive, multi-site

查看:142
本文介绍了clojure,招募,多站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试根据以下内容加载特定模板:服务器名称在请求中返回:

Trying to load a particular template based on what :server-name returns in the request:

(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")))

它适用于localhost它返回/home/usr/rosay/resources/localhost/base.html,但是当我测试对不同的主机说hostname2我看到get-template看到/ home / usr / rosay / resources / hostname2 / base.html,但是当它在浏览器中渲染时总是指向../ resources / localhost / base.html。

It works for localhost which returns /home/usr/rosay/resources/localhost/base.html, but when I test against a different host say "hostname2" I see where get-template is looking at /home/usr/rosay/resources/hostname2/base.html but when it renders in the browser it always points back to ../resources/localhost/base.html.

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

Is there a macro or different way to handle this use-case?

推荐答案

deftemplate 是一个宏,它将模板定义为命名空间中的函数 - 只有一次,在首次求值时。您可以轻松编写一些代码来懒洋洋地创建模板,并通过缓存模板创建后消除一些开销:

As mentioned in the comments, deftemplate is a macro that defines the template as a function in your namespace - only once, when it's first evaluated. You can easily write some code to lazily create the templates, and eliminate some of the overhead by caching the template once it's created:

(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 ...

In your case you'd then be able to say (defservertemplate base "/base.html"....

你可以整理一下,你真正需要知道的是 deftemplate 只是调用模板,您可以直接使用它。

You can probably tidy this up a bit. All you really need to know is that deftemplate just calls template, and you can use that directly if you want.

这篇关于clojure,招募,多站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆