【问题标题】:Composing templates with Hiccup and Compojure使用 Hiccup 和 Compojure 编写模板
【发布时间】:2013-03-15 08:25:26
【问题描述】:

我对 Clojure 和 Compojure 网络开发比较陌生。在我正在构建的玩具示例中,我注意到的第一个问题是 HTML 模板问题。我想支持 Rails 中的部分功能或 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 标记中的内容在它自己的模板文件或函数中,然后能够在我去的时候包含它。例如,我想将它包含在“详细路线”功能中。我看过 Enlive,但我不确定如何将它与 Hiccup 一起使用。任何关于最佳实践的想法都将不胜感激。

【问题讨论】:

    标签: clojure compojure hiccup


    【解决方案1】:

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

    (def head
      [:head
        [:title "Home | Compojure Docs"]
        (include-css "/css/bootstrap.min.css")
         ... ])
    
    (defn routes-in-detail []
      (html5
        head
        [:body
          ... ]))
    

    如果你需要你的 sn-p/partial 来取参数,你可以把它变成一个函数,例如:

    (defn head [title]
      [:head
        [:title title]
        (include-css "/css/bootstrap.min.css")
         ... ])
    
    (defn routes-in-detail []
      (html5
        (head "Routes in detail")
        ... ))
    

    有时您会希望“sn-p”包含多个顶级元素,而不是单个元素。在这种情况下,您可以将它们包装在一个列表中 - hiccup 将内联扩展它:

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

    一旦您意识到打嗝标记是由普通的 clojure 数据结构构成的,您就会发现使用函数操作/构建它既简单又灵活。

    【讨论】:

    • 感谢您的详细解答。这正是我想要的。
    【解决方案2】:

    有一个名为clabango 的新模板库以 Django 模板库为模型,它可能就是您所关心的:https://github.com/danlarkin/clabango

    【讨论】:

      猜你喜欢
      • 2014-03-30
      • 1970-01-01
      • 2011-02-23
      • 2010-11-23
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      相关资源
      最近更新 更多