首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Hiccup和Compojure合成模板

使用Hiccup和Compojure合成模板
EN

Stack Overflow用户
提问于 2013-03-13 22:39:12
回答 2查看 4.3K关注 0票数 7

我是Clojure和Compojure web开发的新手。在我正在构建的玩具示例中,我注意到的第一个问题是HTML模板。我希望能够支持像Rails中的partials,或者Django使用的模板框架。

目前我有:

代码语言:javascript
复制
(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。任何关于最佳实践的想法都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-13 23:32:45

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

代码语言:javascript
复制
(def head
  [:head
    [:title "Home | Compojure Docs"]
    (include-css "/css/bootstrap.min.css")
     ... ])

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

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

代码语言:javascript
复制
(defn head [title]
  [:head
    [:title title]
    (include-css "/css/bootstrap.min.css")
     ... ])

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

有时,您可能希望您的“代码片段”包含多个顶级元素,而不是单个元素。在这种情况下,你可以将它们包装在一个列表中-- hiccup会将其内联展开:

代码语言:javascript
复制
(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数据结构构成的,您就会发现使用函数操作/构建它是简单而灵活的。

票数 11
EN

Stack Overflow用户

发布于 2013-03-15 08:25:27

有一个名为clabango的新模板库,它模仿了Django模板库,它可能就是您正在寻找的模板库:https://github.com/danlarkin/clabango

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15388345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档