CSS在Compojure / Hiccup中的扩展路径失败

时间:2013-06-23 06:19:46

标签: clojure compojure hiccup

我一直在将一些Noir网站转换为Compojure。

我在这里有一个创建页面布局的函数:

(defn layout [title & content]
  (html5
   [:head
    [:title "My Site | " title]
    (include-css "css/main.css")
   [:body
    [:header 
     [:h1 (link-to "/" "My Site")]]
    content]))

这是功能和路线:

(defn home-page []
  (layout
   "Home"
   [:div [:p "Home Page"]])))

(defn article-list []
  (layout
   "Article List"
   [:div [:p "Article List"]])))

(defroutes app-routes
  (GET "/" [] (home-page))
  (GET "/article-list" [] (article-list))

当我打开localhost:3000 / article-list时,所有CSS规则都可以正常工作。

但是,当我尝试扩展URL路径并将程序更改为:

(defn article-list []
  (layout
   "Article List"
   [:div [:p "Article List"]])))

(defn article-one []
  (layout
   "Article One"
   [:div [:p "Article One"]])))

(defroutes app-routes
  (GET "/" [] (home-page))
  (GET "/article-list" [] (article-list)
  (GET "/article-list/article-one" [] (article-one))

转到localhost:3000 / article-list / article-one,我得到所有HTML但CSS规则不再有效。当我检查页面时,css路径包含在<头>元素,但页面上没有样式。

我已经搜索了这个问题的解决方案,但似乎没有任何关于此问题的文章。我也试过拉出路线,以便我有:

(defroutes article-routes
  (GET "/article-list/article-one" [] (article-one))

(defroutes app-routes
  (GET "/" [] (home-page))
  (GET "/article-list" [] (article-list)
  (context "article-list" [] article-routes)

但我有同样的问题。如何让CSS规则在具有扩展路径的页面上工作?

1 个答案:

答案 0 :(得分:5)

您的CSS包含在相对路径中,这意味着当您转到localhost:3000/article-list/article-one时,您的浏览器正在寻找localhost:3000/article-list/css/main.css处的CSS。

解决此问题的最简单方法是将CSS包含在(include-css "/css/main.css")中。开头的/将确保始终搜索localhost:3000/css/main.css