Compojure 模板页面
我有一堆共享相同页眉和页脚的静态 html 文件。我 想要在所有页面上共享此页眉和页脚。现在我 使用以下路由,但它有点难看,我必须处理所有特殊情况。有没有一种简单的方法可以做到这一点,例如 php 的 include 函数?
(defroutes my-app
(GET "/"
(html-with-template
"main.header" "index.body" "main.footer" ))
(GET "/*.html"
(html-with-template
"main.header" (str (params :*) ".body") "main.footer" ))
(GET "/*/"
(html-with-template
(str (params :*) "/folder.header")
(str (params :*) "/index.body")
(str (params :*) "/folder.footer")))
(GET "/*"
(or (serve-file (params :*)) :next))
(ANY "*"
(page-not-found)))
I have a bunch of static html files that share same header and footer. I
would like to share this header and footer across all pages. For now i
use the following routed but it is a bit ugly, and i have to take care of all special cases. Is there an easyway to dothis such as the php's include function?
(defroutes my-app
(GET "/"
(html-with-template
"main.header" "index.body" "main.footer" ))
(GET "/*.html"
(html-with-template
"main.header" (str (params :*) ".body") "main.footer" ))
(GET "/*/"
(html-with-template
(str (params :*) "/folder.header")
(str (params :*) "/index.body")
(str (params :*) "/folder.footer")))
(GET "/*"
(or (serve-file (params :*)) :next))
(ANY "*"
(page-not-found)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我所读到的有关 Compojure 的内容,我认为它不像 PHP 那样对响应正文的“自动前置”和“自动附加”概念具有固有的支持。
我使用过的其他 Web 框架将这一责任委托给他们的模板引擎,而 PHP 则有点模糊了界限。它们通过允许您在此处显式“包含”公共片段,或呈现宏,甚至通过基本形式的继承(此模板扩展了该模板)来实现这一点。
基本上,无论您的 HTML 是静态的还是动态的,模板引擎都允许您进行模块化以获得更好的可维护性。
也就是说,Compojure 似乎没有捆绑一个成熟的 HTML 模板引擎。它确实有一个很好的小型 HTML/XML 特定于域的语言 (DSL),但我认为您所寻找的更多的是可以与 Compojure 一起使用的一流模板引擎。
Enlive 似乎是受 Clojure 启发的模板引擎,点击率最高,但我'我确信还有其他人。考虑到 Clojure 的 JVM 集成,您可能可以从任何受 Java 启发的模板引擎中进行选择 也是如此。
根据您选择的代码,您可能需要编写几行粘合代码来加载、渲染模板并将其传输到 Compojure HTTP 响应中,但您只需编写一次即可在任何地方重复使用。
From what I've read about Compojure, I don't think it has inherent support for the concept of "auto prepend" and "auto append" to the response body like PHP does.
Other web frameworks with which I have experience delegate this responsibility to their templating engine, whereas PHP sort of blurs the lines a bit. They do this by allowing you to explicitly "include" a common snippet here, or render a macro, or even through rudimentary forms of inheritance (this template extends that template).
Basically, whether your HTML is static or dynamic, a templating engine allows you to modularize for better maintainability.
That said, Compojure doesn't appear to have a full-blown HTML templating engine bundled with it. It does have a nice little HTML/XML domain-specific language (DSL), but I think what you are looking for is more of a first class templating engine that can be used along with Compojure.
Enlive seems to be the Clojure-inspired templating engine that get the most hits, but I'm sure there are others. Given Clojure's JVM integration, you can probably pick from any of the Java-inspired templating engines as well.
Depending on which one you pick, there may be a few lines of glue code that you have to write to get your templates loaded, rendered and streamed into the Compojure HTTP responses, but you will write that once and reuse everywhere.