在Compojure中开始使用CSS?

时间:2010-01-25 05:50:17

标签: clojure compojure

我在互联网上找到了一个非常基本的网页,现在我想做一些显而易见的事情并添加一些CSS,这样我就可以建立更好的网页。

  1. 如何包含jQuery以及其他样式表?
  2. 如何包含内联CSS,以便我可以投入 text-align:center ,例如,尝试快速更改?
  3. 常规jQuery包括:

    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"/>
    
    

    没有格式化的基本Hello World服务器:(更新为包含静态路由修复程序,以便其他人可以更快地启动和运行)

    
    (ns hello-world
      (:use compojure))
    
    (defn index
      [request]
        (html
        [:h1 "Hello World"]
        [:p "This is ugly with CSS!"])
        )
    
    (defn hello
      [request]
      (html ""
       [:title "A very long title"]
       [:div.comment
        [:h1 "Hello's Page"]
        [:p "This would look better with some CSS formatting!"]]
    ))
    
    (defroutes greeter
      (GET "/" index)
      (GET "/h" hello)
      (GET "/*"
           (or (serve-file "/opt/compojure/www/public" (params :*)) ;; This is needed to find CSS and js files
           :next))
      (ANY "*"
           (page-not-found) ;;  404.html is in /opt/compojure/www/public/404.html
      ))
    
    
    (run-server {:port 9090}
      "/*" (servlet greeter))
    
    

1 个答案:

答案 0 :(得分:12)

您可以使用以下语法来包含样式属性以指定“内联css样式”:

[:h1 {:style "background-color: black"} "Hello's Page"]

您还可以使用include-css和include-js函数包含样式表标记和javascript。

(defn hello
  [request]
  (html ""
   [:html
     [:head
        [:title "A very long title"]
        (include-css "my css file")
        (include-js "http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js")]
     [:body
       [:div.comment
          [:h1 "Hello's Page"]
          [:p "This would look better with some CSS formatting!"]]]]))

为了提供像css和js文件这样的静态文件,您需要稍微更改路由语句并添加如下内容:

   (GET "/*"
    (or (serve-file "PATH_TO_FILES" (params :*)) :next))

否则,您的本地css文件永远不会被提供。