使用Hiccup和Compojure编写模板

时间:2013-03-13 14:39:12

标签: clojure compojure hiccup

我对Clojure和Compojure网站开发相对较新。在我正在构建的玩具示例中,我注意到的第一个问题是HTML模板。我想支持Rails中的partials或Django使用的模板框架。

目前我有:

(defn index-page []
(html5
    [:head
        [:title "Home | Compojure Docs"]
        (include-css "/css/bootstrap.min.css")
        (include-css "/css/bootstrap-responsive.min.css")]
    [:body
        [:div {:class "container-fluid"}
            [:div {:class "row-fluid"}
                [:div {:class "span2 menu"}]
                [:div {:class "span10 content"}
                    [:h1 "Compojure Docs"]
                    [:ul
                        [:li
                            [:a {:href "/getting-started"} "Getting Started"]]
                        [:li
                            [:a {:href "/routes-in-detail"} "Routes in Detail"]]
                        [:li
                            [:a {:href "/destructuring-syntax"} "Destructuring Syntax"]]
                        [:li
                            [:a {:href "/nesting-routes"} "Nesting Routes"]]
                        [:li
                            [:a {:href "/api-documentation"} "API Documentation"]]
                        [:li
                            [:a {:href "/paas-platforms"} "PaaS Platforms"]]
                        [:li
                            [:a {:href "/example-project"} "Example Project"]]
                        [:li
                            [:a {:href "/example-project-on-cloudbees"} "Example Project on CloudBees"]]
                        [:li
                            [:a {:href "/interactive-development-with-ring"} "Interactive Development with Ring"]]
                        [:li
                            [:a {:href "/emacs-indentation"} "Emacs Indentation"]]
                        [:li
                            [:a {:href "/sessions"} "Sessions"]]
                        [:li
                            [:a {:href "/common-problems"} "Common Problems"]]]
                    (include-js "/js/jquery-1.9.1.min.js")
                    (include-js "/js/bootstrap.min.js")]]]]))

(defn routes-in-detail []
(html5
    [:head
        [:title "Routes in Detail | Compojure Docs"]
        (include-css "/css/style.css")]
    [:body
        [:h1 "Routes in Detail"]]))

我有没有好办法不重复代码?我希望HEAD标签中的东西在它自己的模板文件或函数中,然后能够随时包含它。例如,我想将它包含在'routes-in-detail'功能中。我看过Enlive,但我不确定如何使用Hiccup。任何关于最佳实践的想法都将受到赞赏。

2 个答案:

答案 0 :(得分:11)

您可以将标记的一部分拉出到单独的变量中:

(def head
  [:head
    [:title "Home | Compojure Docs"]
    (include-css "/css/bootstrap.min.css")
     ... ])

(defn routes-in-detail []
  (html5
    head
    [:body
      ... ]))

如果你需要你的代码片段/部分参数,你可以把它变成一个函数,例如:

(defn head [title]
  [:head
    [:title title]
    (include-css "/css/bootstrap.min.css")
     ... ])

(defn routes-in-detail []
  (html5
    (head "Routes in detail")
    ... ))

有时你会希望你的“片段”由多个顶级元素组成,而不是单个元素。在这种情况下,您可以将它们包装在一个列表中 - 打嗝会将其扩展为内联:

(defn head-contents [title]
  (list [:title title]
        (include-css "/css/bootstrap.min.css")
        ... )))

(defn routes-in-detail []
  (html5
    [:head (head-contents "Routes in detail")]
    [:body ... ]))

一旦你意识到hiccup标记是由普通的clojure数据结构构成的,你会发现用函数操作/构建它很容易和灵活。

答案 1 :(得分:0)

有一个名为clabango的新模板库,它是以Django模板库为模型的,可能是你正在关注的内容:https://github.com/danlarkin/clabango