在Compojure中开始使用CSS?

9 clojure compojure

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

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

常规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))

Jos*_*ith 12

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

[:h1 {:style "background-color: black"} "Hello's Page"]
Run Code Online (Sandbox Code Playgroud)

您还可以使用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!"]]]]))
Run Code Online (Sandbox Code Playgroud)

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

   (GET "/*"
    (or (serve-file "PATH_TO_FILES" (params :*)) :next))
Run Code Online (Sandbox Code Playgroud)

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

  • 这个已经过期了.我们需要更好的东西. (2认同)