Tôi tương đối mới đối với phát triển web Clojure và Compojure. Vấn đề đầu tiên mà tôi đã nhận thấy trong ví dụ đồ chơi mà tôi đang xây dựng là việc tạo mẫu HTML. Tôi muốn có hỗ trợ cho một cái gì đó như partials trong Rails, hoặc khuôn khổ templating rằng Django sử dụng.Soạn thảo các mẫu với Hiccup và Compojure
Hiện nay tôi có:
(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"]]))
Có cách nào tốt cho tôi không để lặp lại mã? Tôi muốn nội dung trong thẻ HEAD nằm trong tệp hoặc chức năng mẫu của riêng nó và sau đó có thể bao gồm nó khi tôi đi. Ví dụ, tôi muốn đưa nó vào chức năng 'route-in-detail'. Tôi đã nhìn vào Enlive, nhưng tôi không chắc chắn làm thế nào để sử dụng với Hiccup. Bất kỳ suy nghĩ về thực tiễn tốt nhất ở đây sẽ được đánh giá cao.
Cảm ơn bạn đã trả lời chi tiết. Điều này thật đúng với gì mà tôi đã tìm kiếm. –