Compojure 模板页面

发布于 2024-08-03 22:36:13 字数 617 浏览 10 评论 0原文

我有一堆共享相同页眉和页脚的静态 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-08-10 22:36:13

根据我所读到的有关 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文